鄙人从事C#Winform开发,现有一难题!2张表
表1:test1
Id   Name
表2:test2
Id   Name
结构一样的哈!
功能需求:form画面上有1输入量num,num该量只能是(1|2),画面有一grid绑定的数据源,拖拽方式自己生成了adapter、bandingsource,绑好了设定好样式!现在需要当num=1的时候adapter查的是test1里面的数据,num=2的时候查的是test2的数据
请问该怎么做!------------------------------------------------------
生成2个adpter2个bandingsource
if(num == 1)
{
    grid.DataSource = test1bandingsource;
}
if (num == 2)
{
    grid.DataSource = test2bandingsource;
}
的方法不可行因为这样会改变绑定设置好的样式!
-------------------------------------------------------
我现在就默认绑定的test1也只有一个adapter
在dataset的Designer代码里面有这样一段[System.Diagnostics.DebuggerNonUserCodeAttribute()]
        private void InitCommandCollection() {
            this._commandCollection = new IBM.Data.DB2.DB2Command[1];
            this._commandCollection[0] = new IBM.Data.DB2.DB2Command();
            this._commandCollection[0].Connection = this.Connection;
            this._commandCollection[0].CommandText = "SELECT ID, NAME FROM QJL.BOOK";
            this._commandCollection[0].CommandType = System.Data.CommandType.Text;
        }
是初始化command的
我界面上的num为全局变量所以 修改成
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
        private void InitCommandCollection() {
            this._commandCollection = new IBM.Data.DB2.DB2Command[1];
            this._commandCollection[0] = new IBM.Data.DB2.DB2Command();
            this._commandCollection[0].Connection = this.Connection;
            Form1 f1 = new Form1();
            if (f1.num == 2)
            {
                this._commandCollection[0].CommandText = "SELECT ID, NAME FROM TEST2";
            }
            else
            {
                this._commandCollection[0].CommandText = "SELECT ID, NAME FROM TEST1";
            }
            this._commandCollection[0].CommandType = System.Data.CommandType.Text;
        }
这样的话我界面Form代码里面的全局变量
初始     public num = 1或2;
的时候界面上的数据都能正确现实表test1或test2!
但当操作画面修改num的时候再从新fill,画面上的数据不会改变了!
----------------------------------------------------------------
我感觉是不是这个init终身只会被执行一次!我调试的时候怎么都进不去init方法!从新打开画面之前修改num值数据也会改变就是在运行之后的画面就不管用了 num的值倒是一直在改变就是 不会被 上面的init重新初始坏adapter的command;
我要想在画面运行后修改num重新fill也能出来新的数据该怎么做!我觉得是可行的!但自己道行太浅,恳请各位大侠帮忙!

解决方案 »

  1.   

    能 google baidu 到的也不至于跑这来提问!十万火急等你一句闲话!越想越气!
    公司不要求就做绑定 我自己写还不是想叫他怎么变就怎么变!
    这个东西精通微软生成的设计代码的!人一眼就可以看出来是什么地方不对了!
    我们平常都只是懂原理 都按自己的逻辑思维做事! 现在修改人家微软的了 
    你就来个 很简单 很初级 懒的看! 真火大!
      

  2.   

    不就是两个数据库切换吗?
    connection.ChangeDatabase("");
    我不是高手,路过
      

  3.   

    刚试了下,成功
    拉一个数据源绑定控件bindingSource1
    给bindingSource1添加数据源某数据库,库中两张表users,subjects,这时候会自动生成一些代码,不用管它
    我们只管bindingSource1,因为是它给DataGridView提供数据bindingSource1有个属性:bindingSource1.DataMember,设置它的属性的时候,会重新使数据源FillbindingSource1
    在Form_Load事件中加入dataGridView1.DataSource = bindingSource1;
    添加两个按钮,添加事件处理:private void button1_Click(object sender, EventArgs e)
            {
                bindingSource1.DataMember = "Users";
                
            }private void button2_Click(object sender, EventArgs e)
            {
                bindingSource1.DataMember = "Subjects";
            }
    单击两个按钮,DataGridView中的内容会在两张表间切换
    当然你可以用一个按钮,检查文本框控件中的数字是1还是2来更改bindingSource1.DataMember 很简单的问题,不需要改动自动生成的DataDataSet类(继承于DataSet)
    看下它们的关系:
    DataDataSet类连接数据库并提供两个表(可以在设计器中设计需要的表和字段),
    并给bindingSource1提供数据源:
    this.bindingSource1.DataSource = this.dataDataSet;bindingSource1给dataGridView1提供数据源,具体显示哪张表,由bindingSource1.DataMember决定
    bindingSource1.DataMember字符串类型,就是要显示的表的名称