在Windows Forms中,我正试图通过将
DataGridViewRows
插入其中来手动填充DataGridView
,因此我的代码看起来像这样:DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvArticles);
row.Cells[0].Value = product.Id;
row.Cells[1].Value = product.Description;
.
.
.
dgvArticles.Rows.Add(row);
然而,我希望通过列名而不是索引来添加单元格的值,就像这样:
row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;
但是像这样做会出现一个错误,说它找不到名为“code”的列。 我正在从设计器中设置DataGridView的列,像这样:
我做错了什么吗?我该如何完成我想做的事情?
Name
属性设置为小写的"code",而HeaderText
属性设置为大写的"Code"。在这种情况下,您需要通过索引器引用列Name
而不是HeaderText
。 - Derek W