ComboBox.SelectedValue returns System.Data.DataRowView

ComboBox.SelectedValue returns System.Data.DataRowView

错误是 'No mapping exists from object type System.Data.DataRowView to a known managed provider native type.' 现在 comBoxMuscleGroup.SelectedValuecomBoxTypeFitness.SelectedValue 应该 return ID 但他们 return '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;
           }

       }

属性 ValueMember 和 DisplayMember 是两个字符串,它们应该是提供来自 DataSource 的值的两个字段的名称。
您的查询不包含您为组合命名为 ValueMember 的字段。
因此,它不可能在 属性 SelectedValue 中给出精确的值,而只能给出用于在组合内构建行的 class 的名称(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 属性 之前始终检查是否为 null。