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

前端 未结 5 1444
耶瑟儿~
耶瑟儿~ 2021-01-11 10:13

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

All I have so far is below.

if (Dgv.Columns.Contain         


        
相关标签:
5条回答
  • 2021-01-11 10:30

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

    0 讨论(0)
  • 2021-01-11 10:36
     var dataGridViewColumn = dgv.Columns["Address"];
    
     if (dataGridViewColumn != null && dataGridViewColumn.Visible)
       {
                        //do stuff
       }
    
    0 讨论(0)
  • 2021-01-11 10:42

    You can test the column visibility using the Visible property:

    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.

    0 讨论(0)
  • 2021-01-11 10:45

    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 )
        {
    
        }
    }
    
    0 讨论(0)
  • 2021-01-11 10:49

    The straightforward method:

    if (dgv.Columns.Contains("Address") && dgv.Columns["Address"].Visible)
    {
        // do stuff
    }
    
    0 讨论(0)
提交回复
热议问题