System.Data.DataRowView我该如何解决它

时间:2017-03-07 16:51:18

标签: c#

在我的代码中,我为每个按钮制作了一个方法,例如我有鸡肉和牛肉按钮,并且它们有自己的方法,当我第一次点击鸡肉按钮时,表格加载它的工作原理它将显示在列表框但是当我点击鸡肉后面的牛肉按钮时它会显示一个System.ArgumentException。  如何修复?

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 中。  如何修复它并使其仅在第一次点击时显示?

4 个答案:

答案 0 :(得分:0)

您收到错误,因为当您尝试将ValueMember设置为“Bee_Name”时,列表绑定到tbl_chicken,其中没有Beef_Name,因此 - 正确 - 抱怨。

要解决此问题,请将Datasources设置为tbl_beef,然后设置ValueMember。

答案 1 :(得分:0)

错误是因为值成员是" Beef_ID"和" Chicken_ID"。这些在数据表中不可用。您需要将其更改为" Beef_Name"或包括" Beef_ID"在sql select语句中。 同样,对于相同的列表框,您将设置两个不同的值成员。

答案 2 :(得分: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";  
    }     
}

答案 3 :(得分: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";


        }
    }