We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

returning the valuemember of a datagridviewcombobox.

si2030
si2030 asked
on
Medium Priority
922 Views
Last Modified: 2012-05-06
Hi Experts,

I have a datgridviewcombobox column with a display member and value member. I want to retrieve the valuemember so I can apply the data to file.

I am unsure if I have:

1:applied the valuemember/display member correctly.

The binding source is a LINQ result set with two members:
acc_id and accList.


2: How do I retrieve the valuemember from the selected value in the datagridviewcombobox cell.

What code would I write to retireve this value member?

Kind Regards

Simon
'Create the binding sources.
        Dim BSAccountComboBox As New BindingSource
       
        'Apply the binding sources.
        BSAccountComboBox.DataSource = accountList
     
        With accountComboBox
 
            .Name = "account"
            .DataSource = BSAccountComboBox
            .HeaderText = "Account"
            .DisplayMember = "accList"
            .ValueMember = "acc_id"
            .DropDownWidth = accComboBoxDropDwnWidth
            .DefaultCellStyle.BackColor = Color.WhiteSmoke
            .Width = 220
            .FlatStyle = FlatStyle.Flat
            .MaxDropDownItems = 10
 
 
  With dgvCreditorInvoiceDetail
 
            'Add the accountComboBox column to the DataGridView control.
            .Columns.Add(accountComboBox)
 
        End With

Open in new window

Comment
Watch Question

I'm not entirely sure what you're asking here. I would think that accountComboBox.SelectedValue would give you what you're after.

Author

Commented:
Hi Chaosian,

How would you use accountComboBox.SelectedValue for a specific row... say I have three rows each with a differernt selected value whats the line for "accountComboBox.SelectedValue"?

Simon

Author

Commented:
Say the drop down value or DisplayMember is "10000 Debtors" then the valuemember is "123". I want to obtain the value member - 123 - when this option is selected in the datagridviewcombobox. It may be that there are two or more rows filled in the datagridview then for each cell in the datagiridview in the datagridviewcombobox column I want to get the valuemember not the displaymember...
Just to make sure I understand what you're after, it sounds like there are two issues at hand.
1. How do you get the value for the combo box in a specific row of the grid?
2. How do you get the value rather than the text?
Commented:
Hi Chaosian

I think I have worked out how to obtain these now...

 Dim test = dgvCreditorInvoiceDetail.CurrentCell.Value
        Dim test4 = dgvCreditorInvoiceDetail.Item(0, 0).Value
        Dim test1 = dgvCreditorInvoiceDetail.Rows(0).Cells(0).Value

These all work for the value.

The thing is, you cant obtain it in a cell validating event... it equals zero.

you can however use:
Dim comboBoxColumn As DataGridViewComboBoxColumn = dgvCreditorInvoiceDetail.Columns("account")

comboBoxColumn.Items(e.ColumnIndex).acc_id

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
The validating event issue makes sense -- the selected item likely hasn't been changed yet. Validation would occur first. I would, however, expect to have the correct value in a CellChange event.

Sounds like you've got this well in hand.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.