ComboBox.SelectedValue返回System.Data.DataRowView

时间:2019-02-28 13:50:25

标签: c# combobox selectedvalue datarowview

错误是“从对象类型System.Data.DataRowView到已知的托管提供程序本机类型不存在映射。”现在comBoxMuscleGroup.SelectedValuecomBoxTypeFitness.SelectedValue应该返回Id,但是它们返回'System.Data.DataRowView'。有人可以帮我吗?

代码:

       private void TypeFitness()
       {
           string query = "SELECT Naam FROM TypeFitness";

           using (connection = new SqlConnection(connectionString))
           using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
           {
               DataTable data = new DataTable();
               adapter.Fill(data);

               comBoxTypeFitness.DisplayMember = "Naam";
               comBoxTypeFitness.ValueMember = "FitnessId";
               comBoxTypeFitness.DataSource = data;
           }
       }

       private void MuscleGroup()
       {
           string query = "SELECT Naam FROM MuscleGroup";

           using (connection = new SqlConnection(connectionString))
           using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
           {
               DataTable data = new DataTable();
               adapter.Fill(data);

               comBoxMuscleGroup.DisplayMember = "Naam";
               comBoxMuscleGroup.ValueMember = "MuscleId";
               comBoxMuscleGroup.DataSource = data;
           }
       }

       private void Exercises()
       {
           string query = "SELECT Naam FROM Xercises AS X " +
               "INNER JOIN MG_Exercise AS MGX ON MGX.ExerciseId = X.ExerciseId " +
               "WHERE MGX.MuscleId = @MuscleId AND X.FitnessId = @FitnessId";

           using (connection = new SqlConnection(connectionString))
           using (SqlCommand command = new SqlCommand(query, connection))
           using (SqlDataAdapter adapter = new SqlDataAdapter(command))
           {
               //int MuscleId = ((DataRowView)comBoxMuscleGroup.SelectedValue).Row.Field<int>("MuscleId");
            //int FitnessId = ((DataRowView)comBoxTypeFitness.SelectedValue).Row.Field<int>("FitnessId");

               command.Parameters.AddWithValue("@MuscleId", comBoxMuscleGroup.SelectedValue);
               command.Parameters.AddWithValue("@FitnessId", comBoxTypeFitness.SelectedValue);

               DataTable data = new DataTable();
               adapter.Fill(data);

               clbXcercises.DisplayMember = "Naam";
               clbXcercises.ValueMember = "ExerciseId";
               clbXcercises.DataSource = data;
           }

       }

1 个答案:

答案 0 :(得分:2)

属性ValueMember和DisplayMember是两个字符串,应为两个字段的名称,两个字段提供数据源中的值。
您的查询不包含已为组合命名为ValueMember的字段。
因此,不可能在属性SelectedValue中提供精确的值,而只能在组合(DataRowView)中用于构建行的类的名称

如果要将SelectedValue设置为当前所选项目的MuscleID或FitnessID值,则需要从数据库中提取这些值。

您需要将查询更改为

string query = "SELECT FitnessID, Naam FROM TypeFitness";

string query = "SELECT MuscleID, Naam FROM MuscleGroup";

最后的查询还需要具有ExerciseID

  string query = "SELECT ExerciseID, Naam FROM Xercises AS X " +
       "INNER JOIN MG_Exercise AS MGX ON MGX.ExerciseId = X.ExerciseId " +
       "WHERE MGX.MuscleId = @MuscleId AND X.FitnessId = @FitnessId";

我还建议在使用前始终对SelectedValue属性检查是否为空。