Reputation: 735
Is it possible to add new cell base on column name? How can I possibly do this?
As of now here is my code
foreach (var c in br)
{
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = c.AccountCode });
foreach (var r in c.TBal)
{
//add new cell base on c.AccountCode
}
}
My br has this data
Any ideas? thanks :)
What I want to happen is that AccountCode should be column and TBal balance
should its data under it and so on..
Upvotes: 0
Views: 822
Reputation: 9322
I'm trying to guess here what you want.
You could probably add the Name
property for the column name aside from HeaderText
property and use that column name identity to populate the cells.
{ HeaderText = c.AccountCode, Name = "colAccountCode" }
So, let's just say you are using the name colAccountCode
as your column name for this new column. So, you will have this code that would probably look like this;
foreach (var c in br)
{
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = c.AccountCode, Name = "colAccountCode" });
int rows = dataGridView1.Rows.Count, rowCtr = 0;
if (rows>0) // Make sure there are rows in DataGridView
{
foreach (var r in c.TBal)
{
if (rowCtr < rows) // Make sure that rowCtr is less than Total Rows since index always starts with zero
{
dataGridView1.Rows[rowCtr++].Cells["colAccountCode"].Value = r;
}
}
}
}
Upvotes: 1