How to hide column of DataGridView when using custom DataSource?

前端 未结 7 1680
时光取名叫无心
时光取名叫无心 2020-12-08 20:07

I have a small app in c#, it has a DataGridView that gets filled using:

grid.DataSource = MyDatasource array;

MyClass hold the structure for the

7条回答
  •  不思量自难忘°
    2020-12-08 20:27

    You have to hide the column at the grid view control rather than at the data source. Hiding it at the data source it will not render to the grid view at all, therefore you won't be able to access the value in the grid view. Doing it the way you're suggesting, you would have to access the column value through the data source as opposed to the grid view.

    To hide the column on the grid view control, you can use code like this:

    dataGridView1.Columns[0].Visible = false;
    

    To access the column from the data source, you could try something like this:

    object colValue = ((DataTable)dataGridView.DataSource).Rows[dataSetIndex]["ColumnName"];
    

提交回复
热议问题