ComboBox databinding / DisplayMember

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UmljaGFyZCBCeXNvdXRo?=

    #1

    ComboBox databinding / DisplayMember

    Hi

    I'm having problems getting the correct item to display in a bound combobox
    in my WinForms app. Using the following code:

    With Me.MyComboBox
    .ValueMember = "MyIdField"
    .DisplayMember = "ToString"
    .DataSource = MyDataTable
    End With

    'MyDataTable is a table in the MyExampleDataSe t, which I have the following
    user code in:
    Partial Class MyExampleDataSe t

    Partial Class MyDataTable_Get AllRow

    Public Overrides Function ToString() As String
    'return Id and Description e.g. 12 - Computer Books
    Return String.Format(" {0} - {1}", Me.MyIdField,
    Me.MyDescriptio nField)
    End Function

    End Class
    End Class

    The problem is that the ToString method is never called when databinding, so
    the ComboBox only displays "System.Data.Da taRowView" for each item. I would
    like it to display items such as:
    12 - Computer Books
    14 - Cookery Books
    etc.

    What am I missing here?

    thanks

    Richard
  • =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=

    #2
    RE: ComboBox databinding / DisplayMember

    Richard,

    DisplayMember expects a field (property) name, therefore your overriden
    method (ToString()) is never called. I would suggest you create a calculated
    column on your table, and use it to display - similar to the following:

    MyDataTable.Col umns.Add("Displ ayColumn", GetType(String) ,
    "Convert(MyIdFi eld, System.String) + ' - ' + MyDescriptionFi eld")
    ....
    Me.MyComboBox.D isplayMember = "DisplayCol umn"



    "Richard Bysouth" wrote:
    Hi
    >
    I'm having problems getting the correct item to display in a bound combobox
    in my WinForms app. Using the following code:
    >
    With Me.MyComboBox
    .ValueMember = "MyIdField"
    .DisplayMember = "ToString"
    .DataSource = MyDataTable
    End With
    >
    'MyDataTable is a table in the MyExampleDataSe t, which I have the following
    user code in:
    Partial Class MyExampleDataSe t
    >
    Partial Class MyDataTable_Get AllRow
    >
    Public Overrides Function ToString() As String
    'return Id and Description e.g. 12 - Computer Books
    Return String.Format(" {0} - {1}", Me.MyIdField,
    Me.MyDescriptio nField)
    End Function
    >
    End Class
    End Class
    >
    The problem is that the ToString method is never called when databinding, so
    the ComboBox only displays "System.Data.Da taRowView" for each item. I would
    like it to display items such as:
    12 - Computer Books
    14 - Cookery Books
    etc.
    >
    What am I missing here?
    >
    thanks
    >
    Richard

    Comment

    • Jeffrey Tan[MSFT]

      #3
      RE: ComboBox databinding / DisplayMember

      Hi Richard,

      Yes, just as Sergey pointed out, the Combobox is performing complex
      databinding with the DataTable. Combobox.Displa yMember and ValueMember both
      require the "ColumnName " of the DataTable, you can not specify a method
      name for it.

      I think the solution provided by Sergey should be reliable. This method
      created a separate Expression Column. This Expression Column allows you to
      associate the values of other 2 properties/columns.

      Thanks.

      Best regards,
      Jeffrey Tan
      Microsoft Online Community Support
      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      http://msdn.microsoft.com/subscripti...ult.aspx#notif
      ications.

      Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
      where an initial response from the community or a Microsoft Support
      Engineer within 1 business day is acceptable. Please note that each follow
      up response may take approximately 2 business days as the support
      professional working with you may need further investigation to reach the
      most efficient resolution. The offering is not appropriate for situations
      that require urgent, real-time or phone-based interactions or complex
      project analysis and dump analysis issues. Issues of this nature are best
      handled working with a dedicated Microsoft Support Engineer by contacting
      Microsoft Customer Support Services (CSS) at
      http://msdn.microsoft.com/subscripti...t/default.aspx.
      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no rights.

      Comment

      • Johnny Jörgensen

        #4
        Re: ComboBox databinding / DisplayMember

        Can't you just do the formatting in the Select sentence?

        SELECT SomethingHere, CStr(Idx) + " - " + Description AS MyDescription,
        SomethingElseHe re FROM Books WHERE....

        And then set MyComboBox.Disp layMember = "MyDescript ion"

        Good luck,
        Johnny J.



        "Richard Bysouth" <sloth@nospam.n ospamwrote in message
        news:8592C8A3-9601-4A00-9276-A19AE24B4C20@mi crosoft.com...
        Hi
        >
        I'm having problems getting the correct item to display in a bound
        combobox
        in my WinForms app. Using the following code:
        >
        With Me.MyComboBox
        .ValueMember = "MyIdField"
        .DisplayMember = "ToString"
        .DataSource = MyDataTable
        End With
        >
        'MyDataTable is a table in the MyExampleDataSe t, which I have the
        following
        user code in:
        Partial Class MyExampleDataSe t
        >
        Partial Class MyDataTable_Get AllRow
        >
        Public Overrides Function ToString() As String
        'return Id and Description e.g. 12 - Computer Books
        Return String.Format(" {0} - {1}", Me.MyIdField,
        Me.MyDescriptio nField)
        End Function
        >
        End Class
        End Class
        >
        The problem is that the ToString method is never called when databinding,
        so
        the ComboBox only displays "System.Data.Da taRowView" for each item. I
        would
        like it to display items such as:
        12 - Computer Books
        14 - Cookery Books
        etc.
        >
        What am I missing here?
        >
        thanks
        >
        Richard

        Comment

        • Jeffrey Tan[MSFT]

          #5
          Re: ComboBox databinding / DisplayMember

          Hi Richard,

          Thank you for the feedback.

          Jack has explained to you why binding to "Array" will work with setting to
          "Value" and "ToString". I would like to help you on the last DataSet
          designer not working issue.

          If I remember correct, I have ever got this working in a VS.net2003
          project. Is it possible for you to provide a little sample project to
          demonstrate this problem? I will help you to give it a research. You may
          attach the sample project in a further reply as attachment(thro ugh Outlook
          Express) or you may send it to me at: jetan@online.mi crosoft.com(revmoe
          "online.")

          Thanks.

          Best regards,
          Jeffrey Tan
          Microsoft Online Community Support
          =============== =============== =============== =====
          Get notification to my posts through email? Please refer to
          http://msdn.microsoft.com/subscripti...ult.aspx#notif
          ications.

          Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
          where an initial response from the community or a Microsoft Support
          Engineer within 1 business day is acceptable. Please note that each follow
          up response may take approximately 2 business days as the support
          professional working with you may need further investigation to reach the
          most efficient resolution. The offering is not appropriate for situations
          that require urgent, real-time or phone-based interactions or complex
          project analysis and dump analysis issues. Issues of this nature are best
          handled working with a dedicated Microsoft Support Engineer by contacting
          Microsoft Customer Support Services (CSS) at
          http://msdn.microsoft.com/subscripti...t/default.aspx.
          =============== =============== =============== =====
          This posting is provided "AS IS" with no warranties, and confers no rights.

          Comment

          • =?Utf-8?B?UmljaGFyZCBCeXNvdXRo?=

            #6
            Re: ComboBox databinding / DisplayMember

            Hi Jeffrey

            I'll send you a sample project next week.

            thanks

            Richard

            Comment

            • Walter Wang [MSFT]

              #7
              Re: ComboBox databinding / DisplayMember

              Hi Richard,

              Since Jeffrey will not be in office for several days, would you please also
              CC me when you send the sample project? Thank you for the trouble.


              Regards,
              Walter Wang (wawang@online. microsoft.com, remove 'online.')
              Microsoft Online Community Support

              =============== =============== =============== =====
              When responding to posts, please "Reply to Group" via your newsreader so
              that others may learn and benefit from your issue.
              =============== =============== =============== =====

              This posting is provided "AS IS" with no warranties, and confers no rights.

              Comment

              Working...