繁体   English   中英

如何在DataGridView中查询标题名称?

[英]How to query the DataGridView for the header name?

我想查询datagridview以获取标题名称。 在我获得列表之后,我将循环使用其他内容。 这是填充的datagridview的示例。

feature | serverName1 | serverName2 | serverName3
database| no          | yes         | no
apps    | yes         | no          | yes
services| yes         | yes         | yes

所以在这个例子中,如果我在寻找具有数据库的服务器,它将返回“serverName2”。 或者,如果它有应用程序,它将返回“serverName1,serverName3”等。想法?

public string[] GetHeaders(string feature){
  var result = dataGridView1.Columns
                            .OfType<DataGridViewColumn>()
                            .Where(c=>dataGridView1.Rows
                                                   .OfType<DataGridViewRow>()
                                                   .Any(r=>r.Cells["feature"].Value.ToString() == feature) &&     
                                                   r.Cells[c.Name].Value.ToString() == "yes"))
                            .Select(c=>c.HeaderText)
                            .ToArray();                  
  return result;
}

或这个:

 public string[] GetHeaders(string feature){
    var row = dataGridView1.Rows
                           .OfType<DataGridViewRow>()
                           .FirstOrDefault(r=>r.Cells["feature"].Value.Equals(feature));
    if(row == null) return null;
    return row.Cells.OfType<DataGridViewCell>()
                    .Where(c=>c.Value.Equals("yes"))
                    .Select(c=>c.OwningColumn.HeaderText)
                    .ToArray();
 }

我认为后者更好。

暂无
暂无

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

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