public void ShowDialog(string opendetails, IWin32Window parent)
        {
            SqlConnection conn = new SqlConnection( "Data Source=.\\sqlexpress;Initial Catalog=hardware;Integrated Security=True");
            string sqlString = string.Format("SELECT {0} FROM dexceptional", opendetails);
            SqlCommand cmd1 = new SqlCommand(sqlString, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
            SqlCommandBuilder cmdBuilder1 = new SqlCommandBuilder(adapter);
             
            DataSet ds = new DataSet();
            DataTable table = new DataTable();            conn.Open();            adapter.Fill(table);            BindingSource bindingSource2 = new BindingSource();
            bindingSource2.DataSource = table;            this.dataGridView1.DataSource = bindingSource2;
            bindingNavigator1.BindingSource = bindingSource2;            this.ShowDialog(parent);
            
              
        }        private void toolStripButton1_Click(object sender, EventArgs e)
        {
               这里如何调用 bindingsource2 和adapter

            this.Validate();
            this.bindingSource2.Endedit(); 不行
            this.adapter.Update(table);    不行

            
        }

解决方案 »

  1.   

     BindingSource bindingSource2;
    SqlDataAdapter adapter ;写成全局变量
      

  2.   

    你的问题不明确,因该是调用同一类里的变量,是吗?如果你要调用被ShowDialog赋值的bindingsource2 和adapter 
    ,最好是在类头就声明一个空的 bindingsource2 和adapter 
      

  3.   

    你这样调肯定不行撒,你的数据集和TABLE都在 ShowDialog方法里,属于局部变量.
    把这个DataSet ds = new DataSet(); 
         DataTable table = new DataTable(); 
         写在方法外面,搞成全局的.
      

  4.   

    变量是有生命周期的,你要调用的变量的生命周期只是在ShowDialog这个方法里,所以即使在一个类里也调不到,建议把想要调用的变量设为全局的。
      

  5.   

    bindingsource2 变量的生命周期问题...
    建议有空多看看上学时候的书.
      

  6.   

    写了全局变量,但下面还要用NEW 来新定义,有没有方法直接引用上面的
      

  7.   

    SqlDataAdapter adapter;
    BindingSource bindingSource2 ;
    DataTable table ;
            
    public void ShowDialog(string opendetails, IWin32Window parent) 
            { 
                SqlConnection conn = new SqlConnection( "Data Source=.\\sqlexpress;Initial Catalog=hardware;Integrated Security=True"); 
                string sqlString = string.Format("SELECT {0} FROM dexceptional", opendetails); 
                SqlCommand cmd1 = new SqlCommand(sqlString, conn); 
                adapter = new SqlDataAdapter(cmd1); 
                SqlCommandBuilder cmdBuilder1 = new SqlCommandBuilder(adapter); 
                
                DataSet ds = new DataSet(); 
                table = new DataTable();             conn.Open();             adapter.Fill(table);             bindingSource2 = new BindingSource(); 
                bindingSource2.DataSource = table;             this.dataGridView1.DataSource = bindingSource2; 
                bindingNavigator1.BindingSource = bindingSource2;             this.ShowDialog(parent); 
                
                  
            }         private void toolStripButton1_Click(object sender, EventArgs e) 
            { 
                  这里如何调用 bindingsource2 和adapter             this.Validate(); 
                bindingSource2.Endedit(); //可以
                  adapter.Update(table);    //可以 table也要作为全局变量.
                
            }
      

  8.   

    楼上的,不行啊,提示我要用NEW bindingsource 来新健一次
      

  9.   

    你必须先运行ShowDialog后再toolStripButton1_Click才可以.而且你的ShowDialog是递归的可能最后调用时的结果会有问题吧.或者你在最上面定义时就new了.SqlDataAdapter adapter; 
    BindingSource bindingSource2=new BindingSource(); 
    DataTable table = new DataTable(); 
      

  10.   

    你的数据集和TABLE都在 ShowDialog方法里,属于局部变量. 
    把用到的定义成全局变量。
        DataSet ds ;
        DataTable table;