none
Set Value of a Combobox

    Question

  • Hi,

     

    I have an application in VB.Net 2005. I use a combobox control on a form. I am filling the combobox based on the value user selects from a datagrid. I do not use a datasource or any such thing for this purpose. I want to know how can I set the value property of the combobox.

     

    If anyone has any suggestions please help

     

    Regards

     

    Vineed

    Friday, September 14, 2007 6:40 AM

Answers

  • In the past I have just written a custom class called ComboItemEx that supports a second string that I use for the value... then override the toString()  method to return that second value.  Then when I'm populating the combobox, I add my custom comboitems to them instead.  Check out the sample code I put in this post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2009566&SiteID=1 

     

    I think that should be what you're looking for.

    Friday, September 14, 2007 3:59 PM

All replies

  • I'll assume that since you areusing a combobox that you want to do more than just make the text value of the control equal the cell's content.  I have a simple example below that adds ten numbers to a datagridview and whenever a cell is clicked a search is made for the list item that matches the value.  If found it will set the combobox selected index to that item.  If not found then it adds the value to the list (sorry, no automatic sorting -- that's another lesson) and then selects that item.

     

    All you need to do is create a new project and add a datagridviwe and a combobox to the form through the designer.

    Code Snippet

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim t As New DataTable

    t.Columns.Add("col", GetType(Int32))

    For x As Int32 = 1 To 10

    Dim nr As DataRow = t.NewRow

    nr(0) = x

    t.Rows.Add(nr)

    Next

    t.AcceptChanges()

    DataGridView1.DataSource = t

    End Sub

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter

    If DataGridView1.SelectedCells.Count > 0 Then

    Dim i As Int32 = ComboBox1.Items.IndexOf(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)

    If i = -1 Then

    'The value of the cell was not in the list of items -- add it

    ComboBox1.Items.Add(DataGridView1.SelectedCells(0).Value)

    ComboBox1.SelectedItem = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

    Else

    ComboBox1.SelectedIndex = i

    End If

    End If

    End Sub

    End Class

     

     

     

    Friday, September 14, 2007 12:16 PM
  • you could try something like this:

     

    Code Snippet

    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

      Try

        Dim sValue As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString

        ComboBox1.Items.Add(sValue)

      Catch ex As Exception

        MessageBox.Show(ex.Message)

      End Try

    End Sub

     

     

    hope it helps
    Friday, September 14, 2007 12:17 PM
  • Hi,

     

    Thanks digBoy2000 and Rick for your time. I think I did not explain or elaborate on my question, I apologise for that. See the problem is not getting the value from datagrid into the combobox

     

    The problem is I want to set the "Value" property. I mean, like if I use a DataSource I can set "ValueMember" property (the data set to ValueMember is not visible to user) to a column and then I can use "SelectedValue" property to get the data from the value field. I want to do the same but without a datasource.

     

    In WebControl based combobox it provides the functionality to add Text and Value to the control. I do not know how I can do the same with a Windows based ComboBox.

     

    I hope I have made my question somewhat clear.

     

    Thanks again

     

    Regards

     

    Vineed

    Friday, September 14, 2007 12:28 PM
  • In the past I have just written a custom class called ComboItemEx that supports a second string that I use for the value... then override the toString()  method to return that second value.  Then when I'm populating the combobox, I add my custom comboitems to them instead.  Check out the sample code I put in this post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2009566&SiteID=1 

     

    I think that should be what you're looking for.

    Friday, September 14, 2007 3:59 PM
  • Hi,

     

    First, Thanks for your time. This was excatly something that I was looking for. Thank you very much. Really Appreciated!!!

     

    Thanks again

     

    Regards

     

    Vineed

     

    Saturday, September 15, 2007 4:27 AM