ComboBox首先显示System.Data.DataRowView

时间:2019-03-14 08:48:16

标签: c# database dbnull datarowview

因此在Windows窗体中,我有2个ComboBoxes和一个CheckedListBox用于练习。当您选择适合度(健美操,举重,有氧运动等)和肌肉群(肱三头肌,胸部,背部,前臂等)时,应该从CheckedListBox中进行锻炼。

ComboBoxes'的默认值均为1,因此它应该在CheckedListBox中显示俯卧撑,因为TypeFitness = 1是健美操,而MuscleGroup = 1是前肩

相反,它显示System.Data.DataRowView,但是第二次选择该形式的健美操时,它显示正确的内容。有没有办法使它立即显示练习?

({System.Data.DataRowView仅在上一个选择没有结果时显示)

代码:

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

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

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

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

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

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

    private void Exercises()
    {
        string query = "SELECT X.ExerciseId, X.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))
        {
            SqlParameter param = command.Parameters.AddWithValue("@MuscleId", comBoxMuscleGroup.SelectedValue);
            SqlParameter param2 = command.Parameters.AddWithValue("@FitnessId", comBoxTypeFitness.SelectedValue);

            if(param.Value == null)
            {
                 param.Value = DBNull.Value;
            }
            if(param2.Value == null)
            {
                param2.Value = DBNull.Value;
            }

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

            clbXcercises.DisplayMember = "Naam";
            clbXcercises.ValueMember = "X.ExerciseId";
            clbXcercises.DataSource = Xdata;
        }
    }

1 个答案:

答案 0 :(得分:0)

问题是我将DataSource放在了Exercises()的最后。所以应该是:

clbXcercises.DataSource = Xdata;
clbXcercises.DisplayMember = "Naam";
clbXcercises.ValueMember = "X.ExerciseId";