Results 1 to 6 of 6

Thread: [RESOLVED] Bind List to combobox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Resolved [RESOLVED] Bind List to combobox

    Hi!

    I am working on a new winforms application that is receiving data from a WCF service. The programmer has implemented the services like this:

    Note that this sample is C# but I will be working in VB.NET

    Code:
    [OperationContract]
            [FaultContract(typeof(ServiceFault))]
            [FaultContract(typeof(ValidationFault))]
            [WsdlDoc("Search all cars")]
            [return: MessageParameter(Name = "Cars")]
            List<Car> FindCars(List<SearchCriteriaItem> criterias);
    There seems to be a limitation in the combobox so that it can only bind to name/value collections?

    I use the

    - datasource
    - valuemember
    - displaymember

    properties of the combobox.

    I am pretty new to winforms, I have a background in asp.net

    kind regards
    Henrik

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Bind List to combobox

    I'm fairly sure you can bind it to whatever you want as long as you give it a valid displaymember property (although I'm a little too used to WPF now to be 100&#37; sure about winforms).
    Can you post the code you are using to do the binding and tell us what is happening (do you get any errors etc) ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Bind List to combobox

    That developer needs to brush up on his English for a start. "criterion" is the singular and "criteria" is the plural.

    As for the question, the ComboBox can bind to any IList or IListSource object. The generic List class implements the generic IList interface so no problem. You simply assign the name of the property whose data you want displayed to the DisplayMember, e.g.
    vb.net Code:
    1. myComboBox.DisplayMember = "Name"
    2. myComboBox.ValueMember = "ID"
    3. myComboBox.DataSource = myList
    In actual fact, the DisplayMember and ValueMember accept the names of PropertyDescriptors. PropertyDescriptors are inherently created for properties but a class can create custom PropertyDescriptors, which is why DataColumn names can be used for bound DataTables.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Bind List to combobox

    Thanks, that worked! I have no idea what I did wrong before.

    I have another question I stumbled across recently.

    If I bind a List<of Car> to a GridView, how can I then get the selected row and cast that to an object of type Car? I tried this:

    Code:
    dim c as Car = ctype(myCarsGV.SelectedRows(0).DataBoundItem).Row, Car)
    but get a casting error. The nature of this question is a result to my poor winforms skills. This is no problem in ASP.NET...

    /Henrik

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Bind List to combobox

    Try DirectCast instead of CType (i'll be honest with you here, thats just a total stab in the dark)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Bind List to combobox

    Um, if the DataSource is a List(Of Car) then the DataBoundItem is a Car, so why are you trying to get the Row property? If you bind a Datatable then the DataBoundItem is a DataRowView, so you can get a DataRow from its Row property, but you want a Car, which you already have and which has no Row property. It's just:
    vb.net Code:
    1. Dim c As Car = DirectCast(myCarsGV.SelectedRows(0).DataBoundItem, Car)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width