Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign SelectedValue in a combobox

Status
Not open for further replies.

RSX02

Programmer
May 15, 2003
467
CA
Hi
I have a combo box that I initialise like that
Me.CbPoolType.DataSource() = Me.DatasetPoolType1.Tables("Pool_type")
Me.CbPoolType.ValueMember = DatasetPoolType1.Tables("Pool_type").Columns("Seq").ToString
Me.CbPoolType.DisplayMember = DatasetPoolType1.Tables("Pool_type").Columns("Pool_type").ToString
Me.CbPoolType.Text = ""

I have a datagrid in that same form which show data from another table(JobItem) which table Pool_type is linked to.
In JobItem I keep the seq of the PoolType.

When I double click on the datagrid I would like to display the field "Pool_type" in my combobox (which is the DisplayMember of my combo box).

I tried this
Me.CbPoolType.SelectedValue = DR.Item("Pool_type")

but it seems like I have to open the combo box first if I want it to work. Once I have open the combo box, it's working. If I don't open the combo box once, it will leave the combobox.text property empty

Any idea what I should do?
Thanks in advance
 
The SelectedValue property will be tied to the ValueMember property which for you is currently initialized to the field Pool_type.Seq.

Depending on the structure of the JobItem table, you could do one of two things to display the proper value in your combo box. You could either use
Code:
Me.CbPoolType.SelectedValue = DR.Item("Seq")
if your JobItem table has a sequence field, or you could use
Code:
Me.CBPoolType.SelectedIndex = Me.CbPoolType.FindStringExact(Dr.Item("Pool_type"))
This last example will find the index of the item in the combo box matching the Pool_type string and then set the combo box's selected index to that value.

Jay
 
First, thank you Jay to take time to help me

this line can't work as my DR is a datarow from my table Job_Item and not from my table Pool_type
Me.CbPoolType.SelectedValue = DR.Item("Seq")

I also tried this line
Me.CBPoolType.SelectedIndex = Me.CbPoolType.FindStringExact(Dr.Item("Pool_type"))
But I have the same problem that I had with
Me.CbPoolType.SelectedValue = DR.Item("Pool_type")

As I said, I have to expand the combobox and then it's working. I have no idea why it does this. do you?

Thanks in advance
 
RSX02,

Here's a random thought. I wonder if it's because you're setting the dataSet.Tables(tableName).Column(colName).ToString property to set the the ValueMember and DisplayMember of the combobox. I double-checked the VB .NET documentation on what the DataColumn.ToString property really did and it looks like it returns an Expression object, which is used to filter rows or create calculations. It may explain why you need to expand the combo box in order for things to be properly displayed. Is this what you want to do?

If not, maybe you could try
Code:
Me.CbPoolType.ValueMember = "Seq"
Me.CbPoolType.DisplayMember = "Pool_type"
The combo box object will know what table these column names are bound to since your DataSource is set to Me.DatasetPoolType1.Tables("Pool_type").

Let me know if this works or not.
 
Huel,
I'm sorry but I don't understand the first part of your message. Do you want me to try something else? I tried the "If not" part but it still doesn't work...

I tried this
Me.CbPoolType.ValueMember = "Seq"
Me.CbPoolType.DisplayMember = "Pool_type"
and even after I opened the combo box, I doesn't get any value in my combo box...

Maybe I should try to explain it better.
I have my table JobItem which include these fields
Job
Line
Pool_type

And I have my table Pool_type which include these fields
Seq
Pool_type

Where JobItem.Pool_type is linked to Pool_type.Seq
When I double click on my datagrid (which contain all JobItem for a specific job) It displa those 3 fields (job,line,pool_type) in textbox and combo box(for the pool type field). My problem is that when I double click on the datagrid, it display the job and the line fields but the combobox for the Pool_type is still empty. I don't know why.

Hope it is easier to understand.
Any idea?
Thanks in advance
RSX02
 
I apologize if my last response wasn't very clear. I tried coming up with my own situation which seems to be similar to yours. I work in a laboratory, so I'll use an example from one of my own programs. There are two tables, one called w_vSampleTypes and one called w_vEmailFormats. Here is the structure of the two tables:

w_vSampleTypes
- SampleTypeNumber. Type - Integer.
- SampleTypeName. Type - String.

w_vEmailFormats
- CustomerNumber. Type - Integer.
- SampleTypeNumber. Type - Integer.
- FormatNumber. Type - Integer.

In this example, the SampleTypeNumber in w_vSampleTypes is linked to the SampleTypeNumber in w_vEmailFormats. I bound my datagrid to the w_vSampleTypes table and my combobox to the w_vEmailFormats table. The datagrid displays both fields, while the only value displayed in the combobox is the FormatNumber. When my program starts up, the datagrid and combobox are initially populated, but the combobox is set so that it does not display any values. When I double-click on the datagrid, an e-mail FormatNumber for the SampleTypeNumber selected in the datagrid is displayed in the combobox. For this example, I found that the combobox displayed the proper values after the datagrid was double clicked on without me having to manually expand the combobox. Aside from the table names and fields, is this situation similar to yours? If not, what are the differences?

Code:
    '--------------------------------------------------------
    ' frmMain_Load
    ' Populates the datagrid dgSampTypes with information from the table
    ' w_vSampleTypes and populates the combobox cbFormats with data from
    ' the table w_vEmailFormats.
    ' 
    ' w_vSampleTypes
    '   - SampleTypeNumber. Type - Integer.
    '   - SampleTypeName.   Type - String.
    '
    ' w_vEmailFormats
    '   - CustomerNumber.   Type - Integer.
    '   - SampleTypeNumber. Type - Integer.
    '   - FormatNumber.     Type - Integer.
    '----------------------------------------------------------------------
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'the dataset dsSampTypes will get filled with data from w_vSampleTypes
            daSampType.Fill(dsSampTypes, SAMP_TYPE_VIEW)

            'bind the datagrid to dsSampTypes so that the information from w_vSampleTypes
            'will be displayed
            dgSampTypes.SetDataBinding(dsSampTypes, SAMP_TYPE_VIEW)

            'the dataset dsEFormats will get filled with data from w_vEmailFormats
            daEmailFormat.Fill(dsEFormats, FORMATS_VIEW)

            'bind the combobox to the w_vEmailFormats table
            cbFormats.DataSource = dsEFormats.Tables(FORMATS_VIEW)

            'set the display member to the format number
            cbFormats.DisplayMember = "FormatNumber"

            'set the value member to the sample type number
            cbFormats.ValueMember = "SampleTypeNumber"

            'make sure that the combobox does not display anything at startup
            cbFormats.SelectedIndex = -1
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    '----------------------------------------------------------------------
    ' dgSampTypes_DoubleClick
    ' When a row in the Sample Types datagrid is double-clicked, the combo
    ' box displays the value of the Format Number that corresponds to the
    ' Sample Type selected in the datagrid.
    ' No parameters
    '----------------------------------------------------------------------
    Private Sub dgSampTypes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgSampTypes.DoubleClick
        'column 0 gets the value of SampleTypeNumber.
        'column 1 gets the value of SampleTypeName.
        cbFormats.SelectedValue = dgSampTypes.Item(dgSampTypes.CurrentRowIndex, 0)
    End Sub

I'll be glad to continue helping you out if it's within my abilities.

Jay
 
Jay I didn't try your solution and I didn't solve my problem. Actually I had to change this combo box for a textbox. So I have no longer this problem.
Thanks anyway to all who tried to help me.
rsx02
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top