我有自定義的DataGridView
控件,並且在那個控件中有RefreshGrid()
方法,它通過使用DataSource填充DataGridView
。現在我試圖在DataSource綁定後從DataGridView
刪除幾列,但無法刪除這些列,那些列沒有被刪除,但在DataGridView的末尾添加,當我再次調用RefreshGrid()
方法時,那些列將從DataGridView
中刪除。下面是方法RefreshGrid()
代碼爲什麼DataGridColumn沒有從DataGridView中刪除
public void RefreshGrid()
{
DataTable _table = AccessConnectionManagers.GetDataTableBySQLQuery("select Colm1,Colm2,Colm3 from TableName");
//Data Source Binding with DataGridView
this.DataSource = _table;
if (!string.IsNullOrEmpty("Colm1"))
{
var _colmArray = GridRemoveColumnName.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Where(a => this.Columns.Contains(a)).Select(a => a).ToArray();
foreach (string colm in _colmArray)
{
//Remove column after Source Binding
this.Columns.Remove(colm);
}
}
}
徵集RefreshGrid()
public Form1()
{
InitializeComponent();
myDataGridView1.RefreshGrid();
}
請找出錯誤,並建議我的解決方案。
**現在我打算在DataSource綁定後從DataGridView刪除幾列**請向我們展示在DataSource綁定後刪除這些列的代碼 –
代碼工作正常,您可以始終在綁定後刪除datagridviewcolumns。 –
@MicrosoftDN'this.Columns.Remove(colm);' –