C# 如何检查 DataGridView 是否包含列“x”和列“x”是否可见?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/858629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:30:30  来源:igfitidea点击:

How can I check if a DataGridView contains column "x" and column "x" is visible?

c#winformsdatagridview

提问by

How can I check if a DataGridViewcontains column "x" and column "x" is visible?

如何检查DataGridView包含列“x”和列“x”是否可见?

All I have so far is below.

到目前为止,我所拥有的一切都在下面。

if (Dgv.Columns.Contains("Address") & ....

Thanks

谢谢

采纳答案by Jason

The straightforward method:

直截了当的方法:

if (dgv.Columns.Contains("Address") && dgv.Columns["Address"].Visible)
{
    // do stuff
}

回答by ChrisF

You can test the column visibility using the Visibleproperty:

您可以使用以下Visible属性测试列可见性:

if (column.Visible)
{
    // Do Stuff
}

This will tell you if the column should be displayed.

这将告诉您是否应显示该列。

You can get the column via this call if you know the index:

如果您知道索引,则可以通过此调用获取列:

DataColumn column = dGV.Columns[index];

If the column is displayed but off the screen I don't know how you'd test for that.

如果该列显示但不在屏幕上,我不知道您将如何进行测试。

回答by Chris Dwyer

Loop through the columns, checking the heading (I assume that's what you're looking for) and the Visible property.

遍历列,检查标题(我假设这就是您要查找的内容)和 Visible 属性。

回答by Leonardo Cunha

Firstly verify if the column exists and then you verify its visibility.

首先验证该列是否存在,然后验证其可见性。

Calling the column's property for a column that does not exist will crash.

为不存在的列调用列的属性将崩溃。

if (dgv.Columns.Contains("Address")
{
    if ( dgv.Columns["Address"].Visible )
    {

    }
}

回答by Tee

 var dataGridViewColumn = dgv.Columns["Address"];

 if (dataGridViewColumn != null && dataGridViewColumn.Visible)
   {
                    //do stuff
   }