DataGridView.DataSource不会将三个或更多datagridview绑定在一起

时间:2017-11-16 09:17:43

标签: c# winforms datagridview datatable bindingsource

我有3 DataGridView如下

  1. dataGridViewTASKLIST
  2. dataGridView_USER_ROLES
  3. dataGridViewProjects
  4. 这3个网格用于不同目的。我试图使用sqlDataAdapter和DataTable绑定(使用BindingSource)来自3个不同表,Table1,Table2,Table3(Inside数据库)的数据。运行程序后,只有第一个网格(dataGridViewTASKLIST)填充了行和列,其余两个网格都是空的。

    我的代码如下:

    Console.WriteLine("Binding 1st Grid");
    BindingSource bn = new BindingSource();
    
    bn.DataSource = this.dbCommon.getDataTableAsResult("select * from taskTable");
    dataGridViewTASKLIST.DataSource = bn;
    
    Console.WriteLine("Binding 2nd Grid");
    BindingSource bn1 = new BindingSource();
    
    bn1.DataSource = this.dbCommon.getDataTableAsResult("select * from userTable");
    dataGridView_USER_ROLES.DataSource = bn1;
    
    Console.WriteLine("Binding 3rd Grid");
    BindingSource bn2 = new BindingSource();
    
    bn2.DataSource = this.dbCommon.getDataTableAsResult("select * from projectTable");
    dataGridViewProjects.DataSource = bn2;
    

    其中dbCommon是类DatabaseClass的对象,getDataTableAsResult()是databaseClass中的一个方法

    public DataTable getDataTableAsResult(string cmd)
    {
        sqlDatatable = new DataTable();
        try
        {
            if (this.openConnection())
            {
                sqlAdapter = new SqlDataAdapter(cmd, this.sqlConn);
                sqlAdapter.Fill(sqlDatatable);
            }
    
         }
         catch { }
         finally
         {
            this.closeConnection();
         }
         return sqlDatatable;
    }
    

0 个答案:

没有答案