为什么一个DataGridColumn没有得到从DataGridView中删除 [英] Why DataGridColumn not getting removed from DataGridView
问题描述
我有自定义的 DataGridView的
控制和在控制有 RefreshGrid()
方法填补 DataGridView的
。现在我特林从去除几列 DataGridView的
后的DataSource约束力,但无法删除那些,那些列没有得到删除,但添加在DataGridView的最后,当我打电话 RefreshGrid()
方法又那么这些列也会从 DataGridView的
删除。下面是方法的代码 RefreshGrid()
公共无效RefreshGrid()
{
的DataTable _table = AccessConnectionManagers.GetDataTableBySQLQuery(选择Colm1,Colm2,Colm3从表名);
//数据源与DataGridView的$ B $绑定b this.DataSource = _table;
如果(string.IsNullOrEmpty(Colm1)!)
{
VAR _colmArray = GridRemoveColumnName.Split(新的char [] {',',','}, StringSplitOptions.RemoveEmptyEntries)。凡(一个= GT; this.Columns.Contains(一)。)选择(A =&胺a).ToArray();
的foreach(在_colmArray串科尔姆)
{
//删除列源绑定
this.Columns.Remove(寇)之后;
}
}
}
征集 RefreshGrid()
公共Form1中()
{
的InitializeComponent ();
myDataGridView1.RefreshGrid();
}
请找出错误并提出了我的解决方案。
我找到答案了这个问题。
我需要呼吁窗体加载RefreshGrid()方法而不是表
的构造函数,调用它的形式登录我的问题得到解决之后。但我
不知道为什么,它不是在形式构造的工作。
块引用>
我想您尝试访问不存在的列然而。您正在使用
DataGridView.AutoGenerateColumns
functionnality,即使你设置数据源
属性,则直到显示网格DataGridView的
将无法创建列。这就是为什么它不能在表单构造工作,但在的Form_Load
工程事件或网格显示后。
使用
的Form_Load
也许是可能的解决方法,但我reommand你使用的DataGridView.DataBindingComplete
一>事件被特别设计来处理这种情况。I have custom
DataGridView
control and in that control there isRefreshGrid()
method which fillDataGridView
by using DataSource. Now I am tring to remove few columns from thatDataGridView
after DataSource binding but unable to remove those, those column not getting removed but add at the end of DataGridView, when I callRefreshGrid()
method again then those column get removed fromDataGridView
. Here is code for methodRefreshGrid()
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); } } }
Call for
RefreshGrid()
public Form1() { InitializeComponent(); myDataGridView1.RefreshGrid(); }
Please find the error and suggest me the solution.
解决方案I found answer for this question
I need to call RefreshGrid() method on Form Load not on Form constructor, after calling it on Form Log my problem get solved. But I dont know why it wasnt working on Form constructor.
I guess you try to access columns that do not exist yet. You are using the
DataGridView.AutoGenerateColumns
functionnality and even if you set theDataSource
property, TheDatagridView
won't create columns until the grid is displayed. It's why it doesn't work in form constructor, but works inform_Load
event or after that the grid has been displayed.Using
form_Load
is maybe a possible workaround, but I reommand you to use theDataGridView.DataBindingComplete
event which is especially designed to handle this situation.这篇关于为什么一个DataGridColumn没有得到从DataGridView中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!