我有一個DataGridView
,其數據是從MySQL數據庫中檢索的。我如何只顯示要顯示的列? 一種方法是使用Columns
對象的Visible
屬性和設置dgv.Columns["col"].Visible = false;
C#DataGridView - 選擇要顯示的列
但我想圍繞做它的其他方式。我想選擇要顯示的列,不要選擇要隱藏的列。事情是這樣的: dgv.Columns["col"].Display= true;
我有一個DataGridView
,其數據是從MySQL數據庫中檢索的。我如何只顯示要顯示的列? 一種方法是使用Columns
對象的Visible
屬性和設置dgv.Columns["col"].Visible = false;
C#DataGridView - 選擇要顯示的列
但我想圍繞做它的其他方式。我想選擇要顯示的列,不要選擇要隱藏的列。事情是這樣的: dgv.Columns["col"].Display= true;
試穿DataGridView的AutoGenerateColumns屬性設置爲false
。這將要求您專門設置myColumn.Visible = true
作爲您想要顯示的列。
首先將所有列設置爲不可見,然後設置要顯示的列?
for (int i = 0; i < dgv.Columns.Count; i++)
{
dgv.Columns[i].Visible = false;
}
dgv.Columns["this_one_i_want_to_see"].Visible = true;
dgv.Columns["this_one_i_want_to_see_too"].Visible = true;
好的,但是我現在如何添加列?當我嘗試引用列時,我得到一個NullReferenceException。 –
使用此方法,您需要專門添加想要在DataGridView中顯示的所有列。你可以用問題和/或標記來做到這一點。 https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/add-and-remove-columns-in-the-datagrid-using-the-designer –