1

So in windowsform I have 2 ComboBoxes and one CheckedListBox for exercises. When you select your typefitness (calisthenics, weights, cardio, etc.) and your musclegroup (tricep, chest, back, forearms, etc.) than you should get the exercises out of the CheckedListBox.

Both ComboBoxes' default values are 1, so it should show something like pushups in the CheckedListBox, because TypeFitness = 1 is Calisthenics and MuscleGroup = 1 is Front Shoulder.

Instead it shows System.Data.DataRowView, but the second time I select Calisthenics in the form it shows the right thing. Is there a way to make it so it shows the exercises immediately?

(System.Data.DataRowView only shows when the previous selection didn't have an outcome)

Code:

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;
    }
}
zhrgci
  • 584
  • 1
  • 6
  • 25
  • Ok but here is not the entire code, where are you handling what data you should get from server? – Gabriel Costin Mar 14 '19 at 09:24
  • I don't understand the question. I just initialize all the methods in button events and the form itself. – zhrgci Mar 14 '19 at 09:33
  • Which combobox shows `System.Data.DataRowView` first? What does `first` mean? First time form loads? Or first time combobox is bound to the data table? When the combobox is bound first time to the data table? – Chetan Mar 14 '19 at 09:38
  • `System.Data.DataRowView` show in the `CheckedListBox` when the form loads and when the previous `TypeFitness` and `MuscleGroup` don't have an outcome. So it only shows in the `CheckedListBox` where exercises are put in. (edited it a little bit) – zhrgci Mar 14 '19 at 09:58

1 Answers1

0

The problem was that I put DataSource last Exercises(). So it should be:

clbXcercises.DataSource = Xdata;
clbXcercises.DisplayMember = "Naam";
clbXcercises.ValueMember = "X.ExerciseId";
zhrgci
  • 584
  • 1
  • 6
  • 25