Can't bind a struct to a comboBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DS

    Can't bind a struct to a comboBox

    How do I go about binding a "struct" datatype to a comboBox? When I bind it
    by means of the DataSource, it doesn't give any error, but the comboBox just
    gets populated with "WindowsApplica tion2.Form1+Uni t". Uncommenting the
    DisplayMember line has no effect. Uncommenting the ValueMember results in a
    crash: "Could not bind to the new display member\nParamet er name:
    newDisplaymembe r". How can I get this to work? Thanks in advance.

    Here is the code:

    private void button3_Click(o bject sender, System.EventArg s e)
    {
    Unit[] unit = new Unit[3];
    unit[0].id = 0;
    unit[0].text = "honda";
    unit[1].id = 1;
    unit[1].text = "gm";
    unit[2].id = 2;
    unit[2].text = "toyota";

    MessageBox.Show (unit[0].text); // correctly shows honda

    comboBox1.DataS ource = unit;
    //comboBox1.Value Member = "id";
    //comboBox1.Displ ayMember = "text";
    }

    private struct Unit
    {
    public string text;
    public int id;
    }


  • Morten Wennevik

    #2
    Re: Can't bind a struct to a comboBox

    Hi DS,

    You need properties for the display and value. Change your struct to

    struct Unit
    {
    public string text;
    public int id;
    public string Text
    {
    get{return text;}
    }
    public int Id
    {
    get{return id;}
    }
    }

    ...

    comboBox1.Value Member = "Id";
    comboBox1.Displ ayMember = "Text";

    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    • DS

      #3
      Re: Can't bind a struct to a comboBox


      "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
      news:opsfsuf2v8 klbvpo@pbn_comp uter...[color=blue]
      > Hi DS,
      >
      > You need properties for the display and value. Change your struct to
      >
      > struct Unit
      > {
      > public string text;
      > public int id;
      > public string Text
      > {
      > get{return text;}
      > }
      > public int Id
      > {
      > get{return id;}
      > }
      > }
      >
      > ..
      >
      > comboBox1.Value Member = "Id";
      > comboBox1.Displ ayMember = "Text";
      >
      > --
      > Happy Coding!
      > Morten Wennevik [C# MVP][/color]

      Brilliant! Thank you very much Morten! I spent 3 hours trying various things
      to get it to work with no luck, and now you got it working :). I checked the
      docs and no where do they say that you have to setup properties to access
      the fields in the Struct. In fact, doesn't that sort of go against the
      reason that its "acceptable " to make the fields public and access them
      directly? Am I missing something here?

      As a followup, I seem to have found another mistery. It displays "toyota" in
      the comboBox, and I can grab the correct SelectedValue (which it gives me
      correctly from the ValueMember property), but why is it that when I try to
      grab the SeletecdItem property I once again get
      "WindowsApplica tion2.Form1+Uni t"?

      Thanks again,
      Dan


      Comment

      • Morten Wennevik

        #4
        Re: Can't bind a struct to a comboBox

        On Wed, 13 Oct 2004 19:58:32 -0400, DS <nojunkmail.sig nups@rogers.com >
        wrote:
        [color=blue]
        > Brilliant! Thank you very much Morten! I spent 3 hours trying various
        > things
        > to get it to work with no luck, and now you got it working :). I checked
        > the
        > docs and no where do they say that you have to setup properties to access
        > the fields in the Struct. In fact, doesn't that sort of go against the
        > reason that its "acceptable " to make the fields public and access them
        > directly? Am I missing something here?
        >[/color]
        Can't say
        [color=blue]
        > As a followup, I seem to have found another mistery. It displays
        > "toyota" in
        > the comboBox, and I can grab the correct SelectedValue (which it gives me
        > correctly from the ValueMember property), but why is it that when I try
        > to
        > grab the SeletecdItem property I once again get
        > "WindowsApplica tion2.Form1+Uni t"?
        >
        > Thanks again,
        > Dan
        >[/color]

        The ComboBox stores objects. The displaymember and valuemember specifies
        what parts of that object should be used in the ComboBox but SelectedItem
        returns the whole object, in your case a Unit. I suspect you call
        ToString() on that object, so in effect all you get is the object name
        which is NameSpace.Class Name+StructName .

        You need to cast it back to a Unit and then grab its Text (or text).

        ((Unit)(ComboBo x1.SelectedItem )).Text


        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        • DS

          #5
          Re: Can't bind a struct to a comboBox


          "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
          news:opsfup7edc klbvpo@pbn_comp uter...[color=blue]
          > On Wed, 13 Oct 2004 19:58:32 -0400, DS <nojunkmail.sig nups@rogers.com >
          > wrote:
          >[color=green]
          >> Brilliant! Thank you very much Morten! I spent 3 hours trying various
          >> things
          >> to get it to work with no luck, and now you got it working :). I checked
          >> the
          >> docs and no where do they say that you have to setup properties to access
          >> the fields in the Struct. In fact, doesn't that sort of go against the
          >> reason that its "acceptable " to make the fields public and access them
          >> directly? Am I missing something here?
          >>[/color]
          > Can't say
          >[color=green]
          >> As a followup, I seem to have found another mistery. It displays
          >> "toyota" in
          >> the comboBox, and I can grab the correct SelectedValue (which it gives me
          >> correctly from the ValueMember property), but why is it that when I try
          >> to
          >> grab the SeletecdItem property I once again get
          >> "WindowsApplica tion2.Form1+Uni t"?
          >>
          >> Thanks again,
          >> Dan
          >>[/color]
          >
          > The ComboBox stores objects. The displaymember and valuemember specifies
          > what parts of that object should be used in the ComboBox but SelectedItem
          > returns the whole object, in your case a Unit. I suspect you call
          > ToString() on that object, so in effect all you get is the object name
          > which is NameSpace.Class Name+StructName .
          >
          > You need to cast it back to a Unit and then grab its Text (or text).
          >
          > ((Unit)(ComboBo x1.SelectedItem )).Text
          >
          >
          > --
          > Happy Coding!
          > Morten Wennevik [C# MVP][/color]

          Sweet! You did it again. Thanks! :-) The help and speed is much appreciated!
          Kudos :)

          So just to clarify, should I be making my struct fields private and then
          having accessor properties? Thanks again man.

          Cheers!


          Comment

          • Morten Wennevik

            #6
            Re: Can't bind a struct to a comboBox

            On Fri, 15 Oct 2004 00:40:37 -0400, DS <nojunkmail.sig nups@rogers.com >
            wrote:
            [color=blue]
            >
            > "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
            > news:opsfup7edc klbvpo@pbn_comp uter...[color=green]
            >> On Wed, 13 Oct 2004 19:58:32 -0400, DS <nojunkmail.sig nups@rogers.com >
            >> wrote:
            >>[color=darkred]
            >>> Brilliant! Thank you very much Morten! I spent 3 hours trying various
            >>> things
            >>> to get it to work with no luck, and now you got it working :). I
            >>> checked
            >>> the
            >>> docs and no where do they say that you have to setup properties to
            >>> access
            >>> the fields in the Struct. In fact, doesn't that sort of go against the
            >>> reason that its "acceptable " to make the fields public and access them
            >>> directly? Am I missing something here?
            >>>[/color]
            >> Can't say
            >>[color=darkred]
            >>> As a followup, I seem to have found another mistery. It displays
            >>> "toyota" in
            >>> the comboBox, and I can grab the correct SelectedValue (which it gives
            >>> me
            >>> correctly from the ValueMember property), but why is it that when I try
            >>> to
            >>> grab the SeletecdItem property I once again get
            >>> "WindowsApplica tion2.Form1+Uni t"?
            >>>
            >>> Thanks again,
            >>> Dan
            >>>[/color]
            >>
            >> The ComboBox stores objects. The displaymember and valuemember specifies
            >> what parts of that object should be used in the ComboBox but
            >> SelectedItem
            >> returns the whole object, in your case a Unit. I suspect you call
            >> ToString() on that object, so in effect all you get is the object name
            >> which is NameSpace.Class Name+StructName .
            >>
            >> You need to cast it back to a Unit and then grab its Text (or text).
            >>
            >> ((Unit)(ComboBo x1.SelectedItem )).Text
            >>
            >>
            >> --
            >> Happy Coding!
            >> Morten Wennevik [C# MVP][/color]
            >
            > Sweet! You did it again. Thanks! :-) The help and speed is much
            > appreciated!
            > Kudos :)
            >
            > So just to clarify, should I be making my struct fields private and then
            > having accessor properties? Thanks again man.
            >
            > Cheers!
            >
            >[/color]

            Well, I'm not the one to advice on accessibility, but I would probably
            have made the fields private.

            --
            Happy Coding!
            Morten Wennevik [C# MVP]

            Comment

            Working...