2017-03-07 168 views
-1

在我的代碼中,我爲每個按鈕做了一個方法,例如我有雞肉和牛肉按鈕,他們有自己的方法,當我點擊拳頭時間的雞肉按鈕時裝載它的表單將會顯示在列表框中,但是當我在雞之後點擊牛肉按鈕時,它會向我顯示一個System.ArgumentException。 這將如何解決?System.Data.DataRowView我該如何解決

private void Chicken() 
{ 
    using (connection = new SqlConnection(connectionString)) 
     using (SqlDataAdapter adapter = new SqlDataAdapter("select Chicken_Name,WithOrWithout_Name,Chicken_Price from Tbl_Add a full outer join tbl_Chicken b ON b.Chicken_ID = a.Chicken_ID full outer join Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection)) 
     { 
      DataTable tbl_Chicken = new DataTable(); 
      adapter.Fill(tbl_Chicken); 

      lst_SHowdata.DisplayMember = "Chicken_Name"; 
      lstSHowdata2.DisplayMember = "Chicken_Price"; 

      lst_SHowdata.ValueMember = "Chicken_ID"; 
      lst_SHowdata.ValueMember = "Beef_ID"; 

      lst_SHowdata.DataSource = tbl_Chicken; 
      lstSHowdata2.DataSource = tbl_Chicken; 
     } 
} 

private void Beef() 
{ 
    using (connection = new SqlConnection(connectionString)) 
     using (SqlDataAdapter adapter = new SqlDataAdapter("select Beef_Name,WithOrWithout_Name ,Beef_Price from Tbl_Add a full outer join Tbl_Beef b ON b.Beef_ID = a.Beef_ID full outer join Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection)) 
     { 
      DataTable tbl_Beef = new DataTable(); 
      adapter.Fill(tbl_Beef); 

      lst_SHowdata.DisplayMember = "Beef_Name"; 
      lstSHowdata2.DisplayMember = "Beef_Price"; 

      lst_SHowdata.ValueMember = "Beef_ID"; 
      lst_SHowdata.ValueMember = "Chicken_ID"; 

      lst_SHowdata.DataSource = tbl_Beef; 
      lstSHowdata2.DataSource = tbl_Beef; 
     }  
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    Chicken(); 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    Beef(); 
} 

,我試圖通過使爲使異常

//lst_SHowdata.ValueMember = "Chicken_ID"; 
// lst_SHowdata.ValueMember = "Beef_ID"; 

還挺已經固定線路註釋,以解決它,但我需要點擊按鈕兩次,當我點擊它的第一次它會顯示在列表框System.Data.DataRowView。 如何解決它,並使其只顯示在第一次點擊?

+0

「它給我一個System.ArgumentException」<---任何消息,與一起去? –

+0

System.Windows.Forms.dll中發生未處理的「System.ArgumentException」類型異常。 附加信息:無法綁定到新值成員。 –

+0

這是它顯示的異常時顯示的消息 –

回答

0

糾正我,如果我錯了。這是如何理解它。 您正試圖將數據綁定到按鈕單擊上的列表框。 你有2個列表框控件綁定到。

在分配成員之前先分配數據源。它無法綁定,因爲它尚未被賦予數據源。

private void Chicken() 
    { 
      using (connection = new SqlConnection(connectionString)) 
      using (SqlDataAdapter adapter = new SqlDataAdapter("select Chicken_Name,WithOrWithout_Name,Chicken_Price from Tbl_Add a full outer join tbl_Chicken b ON b.Chicken_ID = a.Chicken_ID full outer join Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection)) 
     { 
      DataTable tbl_Chicken = new DataTable(); 
      connection.Open(); //opens the connection 
      adapter.Fill(tbl_Chicken); 
      connection.Close(); //Closes the connection 

      lst_SHowdata.DataSource = tbl_Chicken; //assigns a datasource 
      lst_SHowdata.DisplayMember = "Chicken_Name"; //assigns display 
      lst_SHowdata.ValueMember = "Chicken_ID"; //assigns id 


      lst_SHowdata2.DataSource = tbl_Chicken; 
      lst_SHowdata2.DisplayMember ="Chicken_Price"; 
      lst_SHowdata2.ValueMember = "Chicken_ID"; 

     } 
    } 

    private void Beef() 
    { 
    using (connection = new SqlConnection(connectionString)) 
    using (SqlDataAdapter adapter = new SqlDataAdapter("select Beef_Name,WithOrWithout_Name ,Beef_Price from Tbl_Add a full outer join Tbl_Beef b ON b.Beef_ID = a.Beef_ID full outer join Tbl_WithORWithout c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection)) 
     { 
     DataTable tbl_Beef = new DataTable(); 
     connection.Open(); 
     adapter.Fill(tbl_Beef); 
     connection.Close(); 

     lst_SHowdata.DataSource = tbl_Beef 
     lst_SHowdata.DisplayMember = "Beef_Name"; 
     lst_SHowdata.ValueMember = "Beef_ID"; 

     lst_SHowdata2.DataSource = tbl_Beef; 
     lst_SHowdata2.ValueMember = "Beef_ID"; 
     lst_SHowdata2.DisplayMember = "Beef_Price"; 
    }  
} 
+0

感謝抱歉的重播,是在C#中的DataBind,因爲當我粘貼的代碼dataBind不識別! ,謝謝我試圖解決它,但我不能 –

+0

它是在C#中。但是,它可能不在控制之中。對不起,我在想ListView。你實際上並不需要數據綁定。你可以評論數據綁定行嗎?並嘗試運行它? – jmag

+0

我已經更新了代碼。由於Databind()用於ASP控件,並且不存在於Forms中。對不起,我應該在發佈前確認。但是我的答案在沒有線路的情況下保持不變。 DataSource先走,然後分配成員。 – jmag

0

因爲當您嘗試將ValueMember設置爲「Bee_Name」時,該列表綁定到tbl_chicken,而tbl_chicken沒有Beef_Name,因此它正確地投訴。

要解決此問題,請將數據源設置爲tbl_beef,然後設置ValueMember。

0

錯誤是因爲valuemember是「Beef_ID」和「Chicken_ID」。這些在數據表中不可用。您需要將其更改爲「Beef_Name」或在sql select語句中包含「Beef_ID」。 也爲同一個列表框設置兩個不同的值成員。

0

正確的代碼^^它

private void Chicken() 
    { 
     using (connection = new SqlConnection(connectionString)) 
     using (SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_Add a full outer join tbl_Chicken b ON b.Chicken_ID = a.Chicken_ID full outer join Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection)) 
     { 
      DataTable tbl_Chicken = new DataTable(); 
      connection.Open(); //opens the connection 
      adapter.Fill(tbl_Chicken); 
      connection.Close(); //Closes the connection 

      lst_Menu.DataSource = tbl_Chicken; //assigns a datasource 
      lst_Menu.DisplayMember = "Chicken_Name"; //assigns display 
      lst_Menu.ValueMember = "Chicken_Name"; 

      lst_worwout.DataSource = tbl_Chicken; 
      lst_worwout.DisplayMember = "WithOrWithout_Name"; 
      lst_worwout.ValueMember = "WithOrWithout_Name"; 

      lst_Price.DataSource = tbl_Chicken; 
      lst_Price.DisplayMember = "Chicken_Price"; 
      lst_Price.ValueMember = "Chicken_Price"; 


     } 
    }