[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-02-19
Medium Priority
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
  • 4
  • 3
LVL 83

Expert Comment

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

Author Comment

ID: 23691186
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

ID: 23691307
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 23697158
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

ID: 23697651
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

ID: 23699271
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

si2030 earned 0 total points
ID: 23879262
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

830 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