简体   繁体   English

检查列是否存在于DataGridView中

[英]Check if column exists in DataGridView

for (int i = 0; i < completeInfoMatches.Count; i++) {
    if (!(databaseGridView.Columns.Contains(e.Node.Parent.Text))) {
        Console.WriteLine(e.Node.Parent.Text);
        databaseGridView.Columns.Add("column" + i, e.Node.Parent.Text);
    }
}

If the column has the name already, I do not want it to be added. 如果该列已经有名称,则我不想添加它。

My code seems like it would work, but for some reason it doesn't? 我的代码似乎可以正常工作,但是由于某种原因却不可行?

It seems that your e.Node.Parent.Text string refers to the HeaderText of the column not to the Name property. 看来您的e.Node.Parent.Text字符串引用该列的HeaderText而不是Name属性。 If this is the case then the code to find if an HeaderText exists with the same value of your e.Node.Parent.Text is the following 在这种情况下,下面的代码用于查找是否存在具有与e.Node.Parent.Text相同值的HeaderText。

for (int i = 0; i < completeInfoMatches.Count; i++) 
{
    if (!(databaseGridView.Columns
              .Cast<DataGridViewColumn>()
              .Any(x => x.HeaderText == e.Node.Parent.Text))) 
    {
        Console.WriteLine(e.Node.Parent.Text);
        databaseGridView.Columns.Add("column" + i, e.Node.Parent.Text);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM