簡體   English   中英

DataGridView生成沒有數據的行

[英]DataGridView generating rows without data

我已經在這里和Google進行了搜索,但仍然找不到答案。 我正在使用Amazon的API,並且正在制作一個簡單的Windows窗體來嘗試在DataGridView中顯示數據。 GridView會為我得到的10個結果生成10行,但沒有用實際數據填充行。 他們只是空白。

下面的代碼是一個返回DataTable的方法(GetResults)。 我沒有顯示所有內容,因為上面有很多代碼來獲取數據。

            DataTable dt = new DataTable();
            dt.Columns.Add("ASIN", typeof(string));
            dt.Columns.Add("Title", typeof(string));

            // write out the results
            foreach (var item in response.Items[0].Item)
            {
                Product product = new Product(item.ASIN, item.ItemAttributes.Title);
                Console.WriteLine(product.ASIN);
                var dr = dt.NewRow();
                dr["ASIN"] = product.ASIN;
                dr["Title"] = product.Title;
                dt.Rows.Add();
            }
            return dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            dgvProducts.AutoGenerateColumns = false;
            dgvProducts.DataSource = GetReults();
        }

我知道它正在獲取信息,因為我正在將其寫入控制台並正確顯示。

我也有這個產品的基本課程:

public class Product
    {
        private string asin;
        private string title;

        public Product() { }

        public Product(string newAsin, string newTitle)
        {
            this.asin = newAsin;
            this.title = newTitle;
        }


        public string ASIN
        {
            get { return asin; }
            set { asin = value; }
        }

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

我嘗試設置AutoGenerateColumns = false並自己設置列數據綁定,但這也沒有做任何事情。

您正在向表中添加一個空行,而不是添加新創建的行。

Change
     dt.Rows.Add();
To
     dt.Rows.Add(dr);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM