[c#-windows form] multi column list box...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • piercy
    New Member
    • Aug 2007
    • 77

    [c#-windows form] multi column list box...?

    i need something simmilar to a multi column listbox. i need to get certain values back form what the user selects. ofr instance if the text of the items in the list say
    smith john 99999 96f
    i would need to get the 99999 part. i could simply replace the space and put them into a array however, some people in the data im using have spaces in surnames so you then end up with and extra space. example:
    al capone john 99999 96f

    where 99999 used to be array[2] it is now array[3]
    i could of course include a for loop to detect for integers but this would make for extremly messy code and if someting goes worng it could cause a lot of errors.

    so i need something like this for instance
    Code:
    |        TEXT            |    Value    |
    smith john 99999 96f     |    99999    |
    al capone john 99991 93g |    99991    |

    so the user will not see the value of the iutem but will see the text.

    it's similar to the way the value attribute works in html..


    any ideas?

    All help is greatly apprieceated :o)
    Piercy
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Use a DataSource.
    You can create a DataTable with all your information and then set it as the datasource for the listbox.
    Set the DisplayMember ="Username" (or whatever you called the column you want them to see)
    Set the ValueMember ="ID" (or whatever you called the column with your integer number)

    Comment

    • piercy
      New Member
      • Aug 2007
      • 77

      #3
      thats sounds like exactly what im looking for however.. the datatable i already have in place (which does already contain the info) doesnt seem to show up in my listbox datasource properties box. ... could be something to do with visual studio 2003... but im going to look into it. if you got any ideas let me know.

      Many thanks,
      Piercy

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You will have to do the leg work yourself in creating the datasource from the database (I think)
        I've never used the UI DataSource thinger so I don't know what you can and cannot do with it

        Comment

        • piercy
          New Member
          • Aug 2007
          • 77

          #5
          Originally posted by Plater
          You will have to do the leg work yourself in creating the datasource from the database (I think)
          I've never used the UI DataSource thinger so I don't know what you can and cannot do with it
          erm.. ive got the datatable from the database its jsut setting the data table as the data source... if im making sense. the datatable already populates itself on the call of a function and was currently populating the listbox in a different way.

          looking through lots of help docs to see if can find the answer..

          thanks
          Piercy

          Comment

          • TRScheel
            Recognized Expert Contributor
            • Apr 2007
            • 638

            #6
            Originally posted by piercy
            i need something simmilar to a multi column listbox. i need to get certain values back form what the user selects. ofr instance if the text of the items in the list say
            smith john 99999 96f
            i would need to get the 99999 part. i could simply replace the space and put them into a array however, some people in the data im using have spaces in surnames so you then end up with and extra space. example:
            al capone john 99999 96f

            where 99999 used to be array[2] it is now array[3]
            i could of course include a for loop to detect for integers but this would make for extremly messy code and if someting goes worng it could cause a lot of errors.

            so i need something like this for instance
            Code:
            |        TEXT            |    Value    |
            smith john 99999 96f     |    99999    |
            al capone john 99991 93g |    99991    |

            so the user will not see the value of the iutem but will see the text.

            it's similar to the way the value attribute works in html..


            any ideas?

            All help is greatly apprieceated :o)
            Piercy
            Depending on the number of rows, this can be time consuming... but... it should work:

            [code=cpp]
            string values[] = YourRow.Split(' ');
            for(int i = 0; i < values.length; i++)
            {
            int temp = 0;
            bool attempt = false;

            attempt = int.TryParse(va lues[i], temp);
            if(attempt && temp.ToString() == values[i])
            {
            // You have a space sepearted item that is all numbers
            }
            }
            [/code]

            Comment

            • piercy
              New Member
              • Aug 2007
              • 77

              #7
              Originally posted by TRScheel
              Depending on the number of rows, this can be time consuming... but... it should work:

              [code=cpp]
              string values[] = YourRow.Split(' ');
              for(int i = 0; i < values.length; i++)
              {
              int temp = 0;
              bool attempt = false;

              attempt = int.TryParse(va lues[i], temp);
              if(attempt && temp.ToString() == values[i])
              {
              // You have a space sepearted item that is all numbers
              }
              }
              [/code]
              doesnt this just do the same as i was doing as in searching for the int in the string via a loop??... i may be wrong as im new to c#..


              Thanks again
              Piercy

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                If you have a datatable called say "mydt" and a listbox called say "mylb".
                AND assume in your datatable you have columns "USERNAME" and "ID".

                Then you would do say:
                Code:
                mylb.DataSource=mydt;
                mylb.DisplayMember="USERNAME";
                mylb.ValueMember="ID";
                And that should be all you need.

                Comment

                • TRScheel
                  Recognized Expert Contributor
                  • Apr 2007
                  • 638

                  #9
                  Originally posted by piercy
                  doesnt this just do the same as i was doing as in searching for the int in the string via a loop??... i may be wrong as im new to c#..


                  Thanks again
                  Piercy
                  Ya but otherwise you need to have a seperator be it a symbol for the integer portion to follow or split them into seperate columns to begin with. You cant just magically have the first integer in the group.

                  Comment

                  • piercy
                    New Member
                    • Aug 2007
                    • 77

                    #10
                    If you have a datatable called say "mydt" and a listbox called say "mylb".
                    AND assume in your datatable you have columns "USERNAME" and "ID".

                    Then you would do say:

                    Code: ( text )
                    mylb.DataSource =mydt;
                    mylb.DisplayMem ber="USERNAME";
                    mylb.ValueMembe r="ID";


                    And that should be all you need.



                    wow.. that helped a lot.. my listbox has been populated with data :D (and faster than it did before... however, is there a way to display two or more columns in the lbData.DisplayM ember property. if need be i will merge the columns earlier on so the data table already has them merged but if its possible it would be good to be able to have the columns SurName and ForeName displayed.


                    can this be done or do i have to merge the datatable columns early on?


                    Many thanks your answers are greatly appreciated.
                    Piercy

                    Comment

                    • piercy
                      New Member
                      • Aug 2007
                      • 77

                      #11
                      actually it was easy enough just to create another column with the values i wanted.. so now i have a display column just for display and i can browse the rest of it from the data table if i want.

                      Thanks very much!!
                      you all been a great help.
                      Piercy

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        I am fairly certain there is a way to make multiple columns show up, only I don't know how.
                        If you merge the columns in your datatable you can use them just fine though.

                        Comment

                        • piercy
                          New Member
                          • Aug 2007
                          • 77

                          #13
                          .. yeah see the only problem with how things have planned out is that i did a load of stuff then it was a sorta joint decision that how i had done it wasnt good. so everything i written has gone to pot near enough and i have to redo the majority of the app.. starting with the drag and drop function which now dont work... this gunna be a bit of a bitch any ideas?

                          Comment

                          Working...