I am trying to show rows in a DataGridView.
Here's the code:
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);
}
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With