2015-09-29 69 views
0

我嘗試從2個不同的表中填充datagridview,它是nonMember和下面的代碼的客戶...但datagridview不顯示除灰色背景之外的任何內容。從不同的表填充Datagridview c#

using (SqlConnection connection = new SqlConnection(ConnectionString)) 
     using (SqlCommand cmd = connection.CreateCommand()) 
     { 
      connection.Open(); 
      cmd.CommandText = "@SELECT tbl_nonMember.*, tbl_customer.customerID AS Expr1, tbl_customer.lname, tbl_customer.fname, tbl_customer.mname, tbl_customer.gender, tbl_customer.age, tbl_customer.membership_type FROM tbl_customer INNER JOIN tbl_nonMember ON tbl_customer.customerID = tbl_nonMember.customerID"; 
      SqlDataAdapter adap = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      adap.Fill(ds); 
      dataGridView2.DataSource = ds.Tables[0].DefaultView; 
     } 
+0

您是否已將DataGridView設置爲自動生成列? – thewisegod

+0

還沒有,我無法在DataGridView屬性中找到自動生成列.. – Josh

回答

1

在設置dataGridView2的數據源之前,嘗試將此添加到您的方法中。

dataGridView2.AutoGenerateColumns = true; 

而你的選擇查詢不應該在關鍵字SELECT的前面有@。它應該是:

cmd.CommandText = "SELECT tbl_nonMember.*, tbl_customer.customerID AS Expr1, tbl_customer.lname, tbl_customer.fname, tbl_customer.mname, tbl_customer.gender, tbl_customer.age, tbl_customer.membership_type FROM tbl_customer INNER JOIN tbl_nonMember ON tbl_customer.customerID = tbl_nonMember.customerID"; 

所以我冒昧地猜測,自動生成後,你的專欄,你的表[0]沒有回,因爲無效的選擇條款的任何行。

+0

完成添加此代碼,但它仍然顯示灰色datagridview – Josh

+0

檢查我更新的答案。 – thewisegod