Reputation: 47
The problem I am facing seems simple to me, but I cannot push past this mental block.
To explain my situation a little better: I am reading in values from an mdf database (Northwind) and passing the ContactName column into a class method that splits the value into first and last names. It then passes these back and I attempt to add them to my DataGridView under the proper columns I created.
My problem is, is that with this current code, it is adding rows for each column, but they are all blank aside from the last one, which shows the last first and last name in the tables column.
I feel that for some reason, the rows that are showing blank might be being overwritten, but I'm not sure. Am I on the right track?
Here is my code:
conn.Open();
SqlDataReader reader = command.ExecuteReader();
searchDataGridView.AllowUserToAddRows = true;
Customer cust = new Customer();
searchDataGridView.ColumnCount = 2;
searchDataGridView.Columns[0].Name = "First Name";
searchDataGridView.Columns[1].Name = "Last Name";
int index = this.searchDataGridView.Rows.Count;
while (reader.Read())
{
string customerid = reader.GetString(0);
string contactname = reader.GetString(2);
cust.NameSplit(contactname);
this.searchDataGridView.Rows.Add();
DataGridViewRow r = searchDataGridView.Rows[rowIndex];
r.Cells["First Name"].Value = cust.FirstName;
r.Cells["Last Name"].Value = cust.LastName;
}
While that seems like a mouthful to me, I hope I explained it clearly enough. If you need more information, please let me know and I would be more than happy to provide it. Thank you :)
Upvotes: 0
Views: 1187
Reputation: 5905
You aren't updating the rowIndex variable as you add rows. This line needs fixing:
rowIndex = this.searchDataGridView.Rows.Add();
Upvotes: 2