2011-10-13 62 views
0

即时通讯使用C#作为前端和MS Access作为后端如何刷新数据网格?

我米仅显示当在组合框中的索引被改变

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     refershGridView(comboBox1.Text); 
    } 

但数据网格当我进行任何更新到数据网格,更新只反映后,我做出选择的指数变化情况

我试图datagidview1.refresh(),也叫我refershGridView(comboBox1.Text)功能隐含

但我的网格视图Refre酒店畲族只有当我做出一个选择的指数变化

代码refershGridView(comboBox1.Text)

private void refershGridView(string tableName) 
    { 

     saveBttnSwitch = 0;//for save button swicth 
     //setting back to intial user interface .. 



     clearVisibilty(); 
     clearall(); 
     button1.Visible = true; 
     button3.Visible = false; 

     label11.Visible = false; 
     try 
     { 
      OleDbConnection mycon = new OleDbConnection(); 

      mycon.ConnectionString = ConnString; 

      //create the database query 
      string query = null; 
      if (tableName == "employee") 
      { 
       query = "SELECT fname,lname,ssn FROM employee"; 

       dataGridView1.Visible = true; 
      } 
      if (tableName == "project") 
      { 


       query = "SELECT pname,pnumber FROM project"; 

       dataGridView1.Visible = true; 

      } 



      //create an OleDbDataAdapter to execute the query 
      OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, mycon); 


      //create a command builder 
      OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter); 

      //create a DataTable to hold the query results 
      DataTable dTable = new DataTable(); 

      //fill the DataTable 
      try 
      { 
       dAdapter.Fill(dTable); 
      } 

      catch (OleDbException exp) 
      { 
       label11.Text = "file couldnt be found...kindly check Db file location "; 
       label11.Visible = true; 
       button1.Visible = false; 

      } 
      // DataGridView dgView = new DataGridView(); 

      //BindingSource to sync DataTable and DataGridView 
      BindingSource bSource = new BindingSource(); 

      //set the BindingSource DataSource 
      bSource.DataSource = dTable; 

      //set the DataGridView DataSource 
      dataGridView1.DataSource = bSource; 
      // dataGridView1.Dock = DockStyle.Fill; 
      dataGridView1.AutoGenerateColumns = true; 

      mycon.Close(); 
     } 
     catch (System.Data.SqlClient.SqlException ex) 
     { 

      throw new InvalidOperationException("Data could not be read", ex); 

     } 

     this.button2.Visible = false; 

    } 
+2

可我知道你当前的代码在refershGridView方法 – Bryan

+0

其清爽,如果再次选择同样的指数。我也尝试调用refreshGridView(combo1.text)在我更新值的地方引用调试模式但不是当我执行时没有调试 – Arunachalam

+0

我已经发布代码 – Arunachalam

回答

0

是否refershGridView(comboBox1.Text);填充DataGrid的?

如果是这样,请从任何其他地方打电话给你想要填充数据的地方。如果SelectedIndexChanged甚至是组合填充的唯一位置,它将不会刷新,除非更改选择。

而@Bryan表示,如果我们看到一些代码,它会更好。

+0

其刷新如果再次选择相同的索引。我也尝试调用refreshGridView(combo1.text)的地方,我更新的值其引用在调试模式,但不是当我执行时没有调试 – Arunachalam

+0

我发布了代码 – Arunachalam

+0

因为我没有看到代码中的任何问题,我用一个简单的表单,网格和组合(当然没有你的按钮可见性和其他东西),使用SQL Server来试用它。工作正常。你是说所有场景在调试模式下工作正常,不是吗? –

0

尝试清除dTabledataGridView1.DataSource您填写的数据表,并设置DataGridView的数据源之前

dTable = new DataTable(); 
dataGridView1.DataSource = nothing;