datagrid刷新问题
DataGrid如果绑定了数据之后,会出现下面的情况。
当用鼠标选中某一编辑框的情况下,对DataGrid的数据源DataSource进行更新,会出现
列xxx不属于table的错误
stacktrace最后是
.... DataRow.CheckColumn .....
.... DataRow.CheckColumn .....
出现的前提情况包括:
1、对datagrid设置了tablestyle,做了对字段的映射
2、在向右拉动了滚动条
3、选中的编辑框不是最后一列
解决方法
在进行刷新之前,先将datagrid的tablestyles清空,然后再加进去。比如:
try
{
this.sqlConnection1.Open();
using(this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter())
{
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
DataSet ds = new DataSet();
this.sqlDataAdapter1.Fill(ds);
ds.Tables[0].TableName = "titles";
if(this.dataGrid1.DataSource != null)
{
this.dataGrid1.TableStyles.Clear();
}
this.dataGrid1.TableStyles.Add(this.dataGridTableStyle1);
this.dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
this.sqlConnection1.Close();
}
{
this.sqlConnection1.Open();
using(this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter())
{
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
DataSet ds = new DataSet();
this.sqlDataAdapter1.Fill(ds);
ds.Tables[0].TableName = "titles";
if(this.dataGrid1.DataSource != null)
{
this.dataGrid1.TableStyles.Clear();
}
this.dataGrid1.TableStyles.Add(this.dataGridTableStyle1);
this.dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
this.sqlConnection1.Close();
}
原因未知
影响包括.net framework 1.0, .net framework 1.1