此代码在窗体打开时没有问题,如果我的字段数变化后,不关闭窗体重新执行如下代码时,没有任何字段显示 //设置可显字段
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());
} }
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());
} }
解决方案 »
- 重装VS2010,遇到握手期间发生错误,求高手解决
- 请教一个listview的colunmsclick事件排序问题?
- SQL查询语句,从2007101到20071105中间的数据怎么写呀,大家帮帮忙
- VS2005调试错误~~高手请指点~~~~
- 请问!--怎么自定义DataGrid中动态生成列的位置啊!?
- 关于随机问题
- 学C#第一大难题:显示一个进程的stdout到一个TextBox(关键字:Process.RedirectStandardOutput)
- url传base64参数搞不定,又跑上来了
- 高分奉上:如何用Visual c# 下一个windows 目录浏览?
- 这个c#程序也编译不了,为啥,请帮助
- Container.DataItemIndex+1的问题
- 请问如何用vs2008 c# 制作一个程序的启动画面呢??
1.注意有没在DataGridView里面cloumn那里设置好相应的字段。
2.GetDgvTitleInfo(obDS.Tables["TStruct_M"], this.DataGridView)有没做相应的移除,比如你删掉了某个字段。你的Table绑定的现实列头。
这样不行吗?
for(int i=0;i<count;i++)
{
BindSourceM.RemoveAt(0);
}
DataGridView.DataSource = BindSourceM;
obDS.Tables["TableM"].Clear();
int count=BindSourceM.Count;
for(int i=0;i <count;i++)
{
BindSourceM.RemoveAt(0);
}
DataGridView.DataSource = BindSourceM;
obDS.Tables["TableM"].Clear();然后在从新绑定 就一点问题都没有了
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());
} }