locked
Bound Listbox Not Updating SelectedIndex RRS feed

  • Question

  • In an attempt to simplify, in general, I have :

    BindingSource.Datasource = DataSet
    ListBox.Datasource = BindingSource
    TableAdapter.ClearBeforeFill = True

    When the form first loads, I load a job using:

    TableAdapter.Fill(DataSet) (Load Job 1, there are multiple items in DataSet)

    ListBox displays all the items correctly and first item is shown as selected:

    ListBox.SelectedIndex = 0 (which is what I want and expect)

    However:

    After the form loads, I use the same:

    TableAdapter.Fill(DataSet) (Load Job 2, there are also multiple items in DataSet)

    ListBox displays all new items correctly and first item is shown as selected, BUT:

    ListBox.SelectedIndex = -1 (that's a minus 1)

    Why doesn't ListBox.SelectedIndex = 0 like it does when the form loads, which is what I want and expect?

    Apparently, when the DataSet is Cleared prior to being ReFilled, the ListBox.SelectedIndex is set to -1, which is understandable. However, once the DataSet has been refilled, it seems like ListBox.SelectedIndex should automatically default to 0, like it does when the form first loads. Afterall, the new items are listed correctly in the ListBox and the first item is shown as being selected.

    I have tried turning off TableAdapter.ClearBeforeFill.
    I have tried manually setting ListBox.SelectedIndex = 0 (throws an exception: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.)

    Nothing works, except to manually click on another item in the ListBox, which then fires the ListBox.SelectedIndex_Changed event and only then does ListBox.SelectedIndex change from -1 to the correct index value.

    However, I need the ListBox.SelectedIndex to default to 0 whenever I Clear and ReFill the DataSet.

    And ideas on what am I doing wrong?

    Thanks,
    George


    Saturday, December 22, 2007 2:33 AM

Answers

  • Actually, in my attempt to simplify my example, I simplified a little too much.

    This is a better example of what I am doing:

    Tbl1TA.ClearBeforeFill = True
    Tbl1BS.DataSource = DataSet
    Tbl1BS.DataMember = DataSet.Tbl1.TableName.ToString

    Tbl1Drp.DataSource = Tbl1BS
    Tbl1Drp.DisplayMember = DataSet.Tbl1.JobNameColumn.ColumnName.ToString
    Tbl1Drp.ValueMember = DataSet.Tbl1.JobIDColumn.ColumnName.ToString
    Tbl1Drp.DropDownStyle = ComboBoxStyle.DropDownList

    Tbl2TA.ClearBeforeFill = True
    Tbl2BS.DataSource = Tbl1BS
    Tbl2BS.DataMember = "FK_Tbl2_Tbl1"
    Tbl2ListBox.DataSource = Tbl2BS
    Tbl2ListBox.DisplayMember = DataSet.Tbl2.Column1.ColumnName.ToString
    Tbl2ListBox.ValueMember = DataSet.Tbl2.Column2.ColumnName.ToString

    There are numerous other Tables with their respective ListBox, each setup in a manner similar to Tbl2.

    Table 1 contains information on all of the different jobs by JobID.
    Table 2 contains information pertaining to only one JobID.

    Since Table 1 contained all jobs, I didn't see a need to ever perform a refill on DataSet.Tbl1

    So, whenever I selected a different job from Tbl1Drp, I passed the JobID to the jobID variable and executed this statement:

    Tbl2TA.FillByJobID(DataSet.Tbl2, jobID)

    Tbl2ListBox would update its contents to reflect the new job information and the first item in the ListBox showed it was selected. However, Tbl2ListBox.SelectedIndex and Tbl2BS.Position would be set to -1 (minus 1) and there was no way I could find to change it to zero.

    Now, when the application first loads, these statements execute...

    Tbl1TA.Fill(DataSet.Tbl1)

    Tbl2TA.FillByJobID(DataSet.Tbl2, jobID)

    ...and all is well.

    So the only difference was that I was not doing another fill on DataSet.Tbl1 when a new job was selected from Tbl1Drp and, apparently, refilling DataSet.Tbl2 -- without refilling DataSet.Tbl1 first -- was somehow breaking Tbl2's binding back to Tbl1.

    Now I execute both statements when a new job is selected from Tbl1Drp...

    Tbl1TA.Fill(DataSet.Tbl1)

    Tbl2TA.FillByJobID(DataSet.Tbl2, jobID)

    ...and Tbl2ListBox.SelectedIndex and Tbl2BS.Position are now both set to zero.

    I don't have enough experience to know if I am doing all this the correct way, but it did solve my problem. In fact, it doesn't seem logical to me to have to refill DataSet.Tbl1 unless it contains new new information, but if that's what it takes.

    George
    Saturday, December 29, 2007 8:48 PM

All replies

  • BTW, I am using VS 2005 Pro.
    Saturday, December 22, 2007 2:47 AM
  • Okay... I think I have it figured out, so disregard this question.
    Saturday, December 22, 2007 3:48 AM
  •  

    Hi, dafringe,

     

    As you have solved your own problem.

    Could you please post the details of your solution?

    I believe that all the community members who meet the same problem could benefit from that.

     

    Thanks,

    Tuesday, December 25, 2007 1:44 AM
  • We are changing the issue type to “Comment” because you have not followed up with the necessary information.

    If you have more time to look at the issue and provide more information, please feel free to change the

    issue type back to “Question” by editing your initial post and changing the radio button at the top of the

    post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that

    the answer can be found and used by other community members having similar questions.

    Hope you have a safe and wonderful holiday!

     

     

    Friday, December 28, 2007 1:41 AM
  • Actually, in my attempt to simplify my example, I simplified a little too much.

    This is a better example of what I am doing:

    Tbl1TA.ClearBeforeFill = True
    Tbl1BS.DataSource = DataSet
    Tbl1BS.DataMember = DataSet.Tbl1.TableName.ToString

    Tbl1Drp.DataSource = Tbl1BS
    Tbl1Drp.DisplayMember = DataSet.Tbl1.JobNameColumn.ColumnName.ToString
    Tbl1Drp.ValueMember = DataSet.Tbl1.JobIDColumn.ColumnName.ToString
    Tbl1Drp.DropDownStyle = ComboBoxStyle.DropDownList

    Tbl2TA.ClearBeforeFill = True
    Tbl2BS.DataSource = Tbl1BS
    Tbl2BS.DataMember = "FK_Tbl2_Tbl1"
    Tbl2ListBox.DataSource = Tbl2BS
    Tbl2ListBox.DisplayMember = DataSet.Tbl2.Column1.ColumnName.ToString
    Tbl2ListBox.ValueMember = DataSet.Tbl2.Column2.ColumnName.ToString

    There are numerous other Tables with their respective ListBox, each setup in a manner similar to Tbl2.

    Table 1 contains information on all of the different jobs by JobID.
    Table 2 contains information pertaining to only one JobID.

    Since Table 1 contained all jobs, I didn't see a need to ever perform a refill on DataSet.Tbl1

    So, whenever I selected a different job from Tbl1Drp, I passed the JobID to the jobID variable and executed this statement:

    Tbl2TA.FillByJobID(DataSet.Tbl2, jobID)

    Tbl2ListBox would update its contents to reflect the new job information and the first item in the ListBox showed it was selected. However, Tbl2ListBox.SelectedIndex and Tbl2BS.Position would be set to -1 (minus 1) and there was no way I could find to change it to zero.

    Now, when the application first loads, these statements execute...

    Tbl1TA.Fill(DataSet.Tbl1)

    Tbl2TA.FillByJobID(DataSet.Tbl2, jobID)

    ...and all is well.

    So the only difference was that I was not doing another fill on DataSet.Tbl1 when a new job was selected from Tbl1Drp and, apparently, refilling DataSet.Tbl2 -- without refilling DataSet.Tbl1 first -- was somehow breaking Tbl2's binding back to Tbl1.

    Now I execute both statements when a new job is selected from Tbl1Drp...

    Tbl1TA.Fill(DataSet.Tbl1)

    Tbl2TA.FillByJobID(DataSet.Tbl2, jobID)

    ...and Tbl2ListBox.SelectedIndex and Tbl2BS.Position are now both set to zero.

    I don't have enough experience to know if I am doing all this the correct way, but it did solve my problem. In fact, it doesn't seem logical to me to have to refill DataSet.Tbl1 unless it contains new new information, but if that's what it takes.

    George
    Saturday, December 29, 2007 8:48 PM