xenogear

当知道了某样知识之后,就会发现其实什么都不知道

datagrid刷新问题

DataGrid如果绑定了数据之后,会出现下面的情况。
当用鼠标选中某一编辑框的情况下,对DataGrid的数据源DataSource进行更新,会出现
列xxx不属于table的错误
stacktrace最后是
.... 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();
   }
原因未知
影响包括.net framework 1.0, .net framework 1.1

posted on 2004-11-27 19:08  什么都不知道  阅读(2994)  评论(4编辑  收藏  举报

导航