Datagridviewcombobox bound, with valuemember/display member causing cellvalidation issues.

Posted on 2009-02-19
Last Modified: 2012-05-06
Hi Experts,

I have  problem with a datagridviewcombobox validation process.

I have the code below which I think works with a simple bound combobox...

But my datagridviewcombobox has a displaymember and valuemember.

The code below does not work... at this point:

"If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then"

I think its because it needs to match against one of the columns in the combobbox drop down list rather than simply the items in a simple dropdown list...

How could you rewrite this to force the "contains" to use the displaymember to compare to the e.formatted...????

Any help with this much appreciated..

Kind Regards

'Create the binding sources.

        Dim BSAccountComboBox As New BindingSource

        Dim BSassetComboBox As New BindingSource

        'Apply the binding sources.

        BSAccountComboBox.DataSource = accountList

        BSassetComboBox.DataSource = refTypeList

        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

        End With

 With dgvCreditorInvoiceDetail

            'Add the accountComboBox column to the DataGridView control.


 End With

Private Sub dgvCreditorInvoiceDetail_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvCreditorInvoiceDetail.CellValidating

Dim comboBoxColumn As DataGridViewComboBoxColumn = dgvCreditorInvoiceDetail.Columns("account")

                        If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then

If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then

                                Dim noRowSelected As DialogResult = MessageBoxEx.Show("You must enter a valid account in the account column...", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information)

                            End If

End if

Open in new window

Question by:si2030
    LVL 83

    Expert Comment

    Use the functions FindString and FindStringExact to search for strings in the displayed members.

    Author Comment

    Hi CodeCruiser,

    Thanks for the response.

    How do I code for this... I am unsure how to refer to the combobox items when using the CellValidating event.

    I have used lines like:

     Dim intIdx = cbx.FindString(cbx.Text)

    However in my case cbx is the datagridviewcombobox... how should I use findstring with displayed members using a datagridviewcombobox?

    LVL 83

    Expert Comment

    FindString works on displaymembers. You just need to provide the search string to the FindStringExact function like
    dim intidx as integer=cbx.findstringexact(cbx.text)

    here is an example

    Author Comment

    Yes this option works fine with a normal combobox. I have already used it anumber of times for an autocomplete sub.

    How does "findstring" work with a datagridviewcombobox in a datagridview column as aposed to a standalone combobox?
    LVL 83

    Expert Comment

    I do not have the experience of working with the datagridviewcombobox but i think if the function exists then it should behave similarly. After all the name says it all. Why dont you give it a try and see what is the outcome.

    Author Comment

    I would but findstring and findstringexact does not exist for this version of the combobox.

    In this instance I was using .contains which does exist but because the dropdown is populated with displaymembers I think the contains does not work with displaymembers/valuemembers.

    Accepted Solution

    I have solved this. I used this:

    If cboItem(1).ToString().Contains(e.FormattedValue) Then

    I needed to set the column for cboItem...

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    754 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

    21 Experts available now in Live!

    Get 1:1 Help Now