为什么我看不到 DataGridViewRow 添加到 DataGridView? [英] Why can't I see the DataGridViewRow added to a DataGridView?
本文介绍了为什么我看不到 DataGridViewRow 添加到 DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 DataGridView 中显示行.
I am trying to show rows in a 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 初始化为:
On form load, I have initialized dataGridView1 as:
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Phone";
执行此代码后,发生了四件值得注意的事情:
After this code is executed, four notable things happen:
- 我可以看到在 dataGridView1 中创建了一个新行.
- 里面没有文字.
- 执行 row.SetValues 方法后,rowset 为 false.
- 行标记值设置正确.
为什么 DataGridView 不显示数据?
Why doesn't the DataGridView show data?
推荐答案
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
这篇关于为什么我看不到 DataGridViewRow 添加到 DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文