returning the valuemember of a datagridviewcombobox.

Posted on 2009-02-18
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

'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.


        End With

Open in new window

Question by:si2030
    LVL 24

    Expert Comment

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

    Author Comment

    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"?


    Author Comment

    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...
    LVL 24

    Expert Comment

    by:Jeff Certain
    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?

    Accepted Solution

    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")


    LVL 24

    Expert Comment

    by:Jeff Certain
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now