none
Datagridview combobox selects using displaymember and not valuemember RRS feed

  • Question

  • HI! . I am making a database application using vb.net.I am using datagridviewcombobox column and binding it to a datasource.The displaymember in the combobox has repeating values although the valuemember is unique.The code is below:

    With pkgsz
            .HeaderText = "Pkg. Size"
            .Width = 60
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
            .Items.Clear()
            .DataSource = dt51
            .DisplayMember = "SIZE"
            .ValueMember = "CODE"
            .DataPropertyName = "Pkg. Size"
    End With

    I have the following values in the datatable

    SIZE   CODE   UNIT
      25       1        Kgs
    250       2       Gms
    250       3        Kgs
    ...

    'Size' being the displaymember, and 'Code' being the valuemember, I have two 250's in the combobox. On selecting either of them the Code '2' is being set as the value in the datagridview combobox column.

                      It seems dot net is selecting value on the basis of sorted displaymember and not valuemember, as the case should be...
    Have tried various solutions but not getting thru.

                      Your help shall be most appreciated
    thanks

    Saturday, July 14, 2012 1:48 PM

Answers

  • Hi Prate,

    Welcome to the MSDN Forum.

    Please try the following code:

        Private Sub DuplicateValue_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("SIZE")
            dt.Columns.Add("CODE")
            dt.Columns.Add("UNIT")
            dt.Rows.Add("25", "1", "Kgs")
            dt.Rows.Add("250", "2", "Gms")
            dt.Rows.Add("250", "3", "Kgs")
            dt.Rows.Add("250", "4", "Gms")
            dt.Rows.Add("250", "5", "Kgs")
    
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "SIZE"
            ComboBox1.ValueMember = "CODE"
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Console.WriteLine(ComboBox1.SelectedValue)
        End Sub

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 20, 2012 5:17 AM
    Moderator

All replies

  • Hi Prateekruia,

    Welcome to the MSDN Forum.

    What is your problem?

    >> It seems dot net is selecting value on the basis of sorted displaymember and not valuemember, as the case should be...

    What does this exactly mean?

    As far as I know, when the displaymember is sorted, the valuemember will be sorted too.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 16, 2012 8:40 AM
    Moderator
  • The problem is as below :

    I have the following values in the datatable - dt51

    SIZE   CODE   UNIT
      25       1        Kgs
    250       2       Gms
    250       3        Kgs
    ...

    I have a combobox 'pkgsz' which is bound to this datatable

    pkgsz.DataSource = dt51

    The column 'SIZE' is the displaymember and 'CODE' is the valuemember of combobox 'pkgsz'

    pkgsz.DisplayMember = "SIZE"

    pkgsz.ValueMember = "CODE"

    When I select the second value ie. '250' in the combobox, the value of combobox becomes '2'

    When I select the first value ie. '25' in the combobox, the value of combobox becomes '1'

    Now, when I select the third value ie. again '250' in the combobox, the value of combobox still becomes '2'

    The valuemember of the third item ('250') is '3', so the value of combobox should become '3' in this case, but it becomes '2'

    Can you suggest a way so that for the third item, for which the valuemember is '3', the value of combobox also becomes '3'

    Any help shall be greatly appreciated.

    Thanks

    Prateek Ruia

    Thursday, July 19, 2012 1:24 PM
  •  Hi, Because you have duplicate values in the column being compared to the contents of the combobox the first occurrence to be true will be the one used. 

      It appears from the data that what you are trying to separate is the unit of measure because 250 Gms is not 250 Kgs.

    What if your data table contained the following?

    SIZE   CODE   UNIT
      25       1        Kgs
    250       2        Gms
    250       3        Kgs
    250       4        Gms
    250       5        Kgs

    In this case you would have to use the unit of measure as the primary combo box selection(because the 250 means nothing by itself and only means something if the unit of measure is used in conjunction with the size). Say you wanted all the 250 Kgs values in the data table. Then you would have to use the unit and the size. The unit would be the what is selected in the combobox and then loop through the data table to get the size and compare this to what ever you are looking for. When you have more then one of a size then you would have to do this for each instance of that size.

     For example say I wanted to display all the 250 Kgs or 50 Gms sizes  to a label on the screen. I would do something like the following code.

    This code uses a combobox and label on form1.

    Public Class Form1
        Dim data(5, 1, 1) As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            data(0, 0, 0) = "50"
            data(0, 0, 1) = "1"
            data(0, 1, 0) = "Kgs"
            data(1, 0, 0) = "50"
            data(1, 0, 1) = "2"
            data(1, 1, 0) = "Gms"
            data(2, 0, 0) = "250"
            data(2, 0, 1) = "3"
            data(2, 1, 0) = "Kgs"
            data(3, 0, 0) = "250"
            data(3, 0, 1) = "4"
            data(3, 1, 0) = "Gms"
            data(4, 0, 0) = "250"
            data(4, 0, 1) = "5"
            data(4, 1, 0) = "Gms"
            data(5, 0, 0) = "250"
            data(5, 0, 1) = "6"
            data(5, 1, 0) = "Kgs"
            Label1.Text = ""
        End Sub
    
        Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            Label1.Text = ""
            For x = 0 To 5
                If data(x, 1, 0) = "Kgs" And data(x, 0, 0) = "250" And ComboBox1.SelectedItem = "Kgs" Then
                    Label1.Text = Label1.Text & data(x, 0, 0) & " | " & data(x, 0, 1) & " | " & data(x, 1, 0) & " | "
                End If
                If data(x, 1, 0) = "Gms" And data(x, 0, 0) = "50" And ComboBox1.SelectedItem = "Gms" Then
                    Label1.Text = Label1.Text & data(x, 0, 0) & " | " & data(x, 0, 1) & " | " & data(x, 1, 0) & " | "
                End If
            Next
        End Sub
    End Class
    
     This gives an example for you of how data can be used. I did not set up a database and table because I did not have that time so for speed I substituted and used an array for the database and table. 


    Curtis


    Curtis

    Thursday, July 19, 2012 3:18 PM
  • Hi Prate,

    Welcome to the MSDN Forum.

    Please try the following code:

        Private Sub DuplicateValue_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("SIZE")
            dt.Columns.Add("CODE")
            dt.Columns.Add("UNIT")
            dt.Rows.Add("25", "1", "Kgs")
            dt.Rows.Add("250", "2", "Gms")
            dt.Rows.Add("250", "3", "Kgs")
            dt.Rows.Add("250", "4", "Gms")
            dt.Rows.Add("250", "5", "Kgs")
    
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "SIZE"
            ComboBox1.ValueMember = "CODE"
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Console.WriteLine(ComboBox1.SelectedValue)
        End Sub

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 20, 2012 5:17 AM
    Moderator