此代码在窗体打开时没有问题,如果我的字段数变化后,不关闭窗体重新执行如下代码时,没有任何字段显示        //设置可显字段
        private void btnSetting_Click(object sender, EventArgs e)
        {
            Frm_TS_TableStruct obFrm = new Frm_TS_TableStruct(TableNameM);
            obFrm.ShowDialog();            if (obFrm.DialogResult == DialogResult.OK)
            {
                //重新加载
                DataGridView.AutoGenerateColumns = true;
                this.DataGridView.Columns.Clear();
                FetchStuct(TableNameM);
                strWhereM = "Where 1=0 ";
                LoadDataMaster(TableID_M, strWhereM);                //獲取主、明?體網格字段信息(標題/列?等)
                GetDgvTitleInfo(obDS.Tables["TStruct_M"], this.DataGridView);
            }
        }
        //加载主表数据
        private void LoadDataMaster(string mTableName ,string strCriteria )
        {
            try
            {
                //获取用户指定条件的记录
                string strSQLM;
                strSQLM = "Select " + strMasterFields + " From " +mTableName +" Where " + strCriteria;                obAD_dgvM = new SqlDataAdapter(strSQLM, obConn);
                if (obDS.Tables["TableM"] != null)
                {
                    obDS.Tables["TableM"].Clear();
                }
                obAD_dgvM.Fill(obDS, "TableM");                BindSourceM.DataSource = obDS;
                BindSourceM.DataMember = "TableM";                this.DataGridView.DataSource = BindSourceM;
                bindingNavigator.BindingSource = BindSourceM;            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }        }

解决方案 »

  1.   

    最简单的方法,是先让DataSource = null; 再给它附值。
      

  2.   

    检查2点
    1.注意有没在DataGridView里面cloumn那里设置好相应的字段。
    2.GetDgvTitleInfo(obDS.Tables["TStruct_M"], this.DataGridView)有没做相应的移除,比如你删掉了某个字段。你的Table绑定的现实列头。
      

  3.   

    我不是已经写了this.DataGridView.Columns.Clear();
    这样不行吗?

      

  4.   

                int count=BindSourceM.Count;
                for(int i=0;i<count;i++)
                {
                     BindSourceM.RemoveAt(0);
                }          
                DataGridView.DataSource = BindSourceM;
                obDS.Tables["TableM"].Clear();
      

  5.   

    先执行下面的代码:
         int count=BindSourceM.Count; 
          for(int i=0;i <count;i++) 
             { 
                BindSourceM.RemoveAt(0); 
             }          
           DataGridView.DataSource = BindSourceM; 
           obDS.Tables["TableM"].Clear();然后在从新绑定 就一点问题都没有了
      

  6.   

    各位,解决了谢谢        //加载主表数据
            private void LoadDataMaster(string mTableName ,string strCriteria )
            {
                try
                {
                    //获取用户指定条件的记录
                    string strSQLM;
                    strSQLM = "Select " + strMasterFields + " From " +mTableName +" Where " + strCriteria;                obAD_dgvM = new SqlDataAdapter(strSQLM, obConn);
                    if (obDS.Tables["TableM"] != null)
                    {
                        //obDS.Tables["TableM"].Clear(); 改为如下
                        obDS.Tables.Remove("TableM");

                    }
                    obAD_dgvM.Fill(obDS, "TableM");                BindSourceM.DataSource = obDS;
                    BindSourceM.DataMember = "TableM";                this.DataGridView.DataSource = BindSourceM;
                    bindingNavigator.BindingSource = BindSourceM;            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }        }