0

I am working with Win forms and VB.Net

Private Sub LoadPackageNames()
    lbPackageName.ValueMember = "Value"
    lbPackageName.DisplayMember = "Label"
    lbPackageName.DataSource = ReportRunner.GetReportPackages()
End Sub

Private Sub lbPackageName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbPackageName.SelectedIndexChanged
    lbRunTime.DisplayMember = "Label"
    lbRunTime.ValueMember = "Value"
    lbRunTime.DataSource = ReportRunner.GetReportTimes(lbPackageName.SelectedValue)


    clbReportName.DisplayMember = "Label"
    clbReportName.ValueMember = "Value"
    clbReportName.DataSource = ReportRunner.GetReportNames(lbPackageName.SelectedValue)       
End Sub

The code is simple. Clicking on different options in the list box lbPackageName I get different results. Sometimes I get the values expected in clbReportName sometimes I get System.Data.DataRow. lbPackageName and lbRunTimes, for now, always populate correctly.

Is there something different about Check List Boxes?

enter image description here

Here is the query that gets returned as a DataTable

SELECT A.ReportName AS Label, A.ReportName AS Value
FROM ReportRunnerPackageToReportAssociation A
WHERE A.PackageName = @PackageName 

An Interesting find: Sometimes the DisplayMember is being set back to blank? enter image description here

Mike
  • 5,918
  • 9
  • 57
  • 94
  • *Where* and *how* are you getting results? How is your "reports" list bound? – Crono Mar 17 '15 at 17:07
  • When the form loads LoadPackageNames is called, I see the results on the form itself – Mike Mar 17 '15 at 17:09
  • 1
    You would have to show us the GetReportNames function. – LarsTech Mar 17 '15 at 17:09
  • It is a simple sql call.. again the frustrating thing is if I select some items it works, If I select the last and sometimes the third I get the result now shown. If I pause to look at the table in the debugger OR I select the same item twice the form displays correctly ... I will add the query – Mike Mar 17 '15 at 17:13
  • 1
    The CheckedListBox doesn't support a DataSource, which is why it was hidden from intellisence. You can work around it by setting the DisplayMember and ValueMembers *after* you set the DataSource, but it's probably better to just add the items yourself. – LarsTech Mar 17 '15 at 17:23
  • I will try what you say but .Datasource does come up in my intellisence and according to the Documentation it is public https://msdn.microsoft.com/en-us/library/k86hzt9t(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 – Mike Mar 17 '15 at 17:28
  • 1
    Datasource is set to `EditorBrowsableState.Never` in the ref source but it does pass it to the base class. There may be an with using it and the drawmode used to draw items as checks. I might use a DGV and fiddle with the view to apply filters rather than re-issue the query – Ňɏssa Pøngjǣrdenlarp Mar 17 '15 at 17:31
  • So setting the properties after seem to work, and it also works with the list boxes also with one side effect. The SelectedIndexChange fires before the properties are set so therefore the SelectedValue is "DataRow". Do you have a recommendation on how to solve this. I guess I can set a flag but I was wondering if there was a correct pattern on how to implement this – Mike Mar 17 '15 at 17:32
  • 1
    changing the datasource always clears the selectedxxxx collections – Ňɏssa Pøngjǣrdenlarp Mar 17 '15 at 17:36
  • @Plutonix Thank you, a DGV may be a better approach overall rather than making round trips back the database – Mike Mar 17 '15 at 17:37
  • @Plutonix I don't know if that is true for List Boxes it seems that the first Item is always in SelectedValue / SelectItem. Instead of a String the Item is a DataRow – Mike Mar 17 '15 at 17:39
  • 1
    SelectedItems and SelectedIndicies are cleared when the DataSource changes because the items in the list have changed and so those collections are invalid. I dont know about SelectedItem, but that is why SelectedIndex changed gets fired when you change it. There is also a note in the ref source about resetting the DisplayMember to "" when the DS changes. – Ňɏssa Pøngjǣrdenlarp Mar 17 '15 at 17:59

1 Answers1

0

In answer to your question:

Is there something different about Check List Boxes?

Yes.

When binding to the "ValueMember" property of the checked list box, the value that is bound must be a Boolean.

You can't have the value be "2/18/2013 3:21 PM" it should be "true" or "false".

So you will probably want to bind the "DisplayMember" to the "Label" and get the indexes of the selected items to get the data you want. Or, in your case, since the label and value are the exact same data "A.ReportName" you could get away with getting the text of the checked items during an event.

There is a good example of binding to the checked list box here.

Community
  • 1
  • 1
CodeBob
  • 775
  • 7
  • 14