[英]How to add new cell by column name in datagridview
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.. 我想发生的事情是AccountCode应该是列,而
TBal balance
应该是其下的数据,依此类推。
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
属性之外的列名添加Name
属性,并使用该列名标识填充单元格。
{ HeaderText = c.AccountCode, Name = "colAccountCode" }
So, let's just say you are using the name colAccountCode
as your column name for this new column. 因此,假设您使用名称
colAccountCode
作为此新列的列名。 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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.