我正在尝试在 DataGridView 中显示行。
这是代码:
foreach (Customers cust in custList)
{
string[] rowValues = { cust.Name, cust.PhoneNo };
DataGridViewRow row = new DataGridViewRow();
bool rowset = row.SetValues(rowValues);
row.Tag = cust.CustomerId;
dataGridView1.Rows.Add(row);
}
在表单加载时,我已将 dataGridView1 初始化为:
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Phone";
执行此代码后,会发生四件值得注意的事情:
- 我可以看到 dataGridView1 中创建了一个新行。
- 里面没有文字。
- row.SetValues 方法执行后,rowset 为 false。
- 行标记值设置正确。
为什么DataGridView不显示数据?
List<customer> custList = GetAllCustomers();
dataGridView1.Rows.Clear();
foreach (Customer cust in custList)
{
//First add the row, cos this is how it works! Dont know why!
DataGridViewRow R = dataGridView1.Rows[dataGridView1.Rows.Add()];
//Then edit it
R.Cells["Name"].Value = cust.Name;
R.Cells["Address"].Value = cust.Address;
R.Cells["Phone"].Value = cust.PhoneNo;
//Customer Id is invisible but still usable, like,
//when double clicked to show full details
R.Tag = cust.IntCustomerId;
}
http://aspdiary.blogspot.com/2011/04/adding-new-row-to-datagridview.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)