Solved

Need assistance with DataGridViewComboBoxCoumn

Posted on 2015-02-04
2
40 Views
Last Modified: 2015-02-09
Experts, I am experiencing a strange thing with my DataGridViewComboBoxColumn. Below is the code I am using. I need to set the value in a datatable to the selected index of the combo box. Everything works fine until it encounters this line.

row("GroupID") = combo.SelectedIndex.

As soon as it hits this line it immediately exits the method. Also, it sets the combo box selected index to 0 (zero). I can see my selection in the combo box very briefly before it changes back to an index of 0. I know it is this line because I can comment it out and then everything begins working correctly. I have also tried the "SelectionChangeCommitted" event and while it should only fire once, this line is causing it to fire as many as ten times.

Can anyone help me with this problem?
DataGridView1.DataSource = ds.tables("dtProjectItems")
       
 Dim dt As New DataTable
        With dt
            .Columns.Add("ID", GetType(Integer))
            .Columns.Add("Text", GetType(String))
            .Rows.Add(0, "NONE")
            .Rows.Add(1, "OPTION")
            .Rows.Add(2, "SPARE")
            .Rows.Add(3, "CONSUMABLE")
        End With
        Dim ccolumn As New DataGridViewComboBoxColumn
        With ccolumn
            .DataSource = dt
            .DisplayMember = "Text"
            .ValueMember = "ID"
            .Name = "COMBO"
        End With    

Private Sub DataGridView2_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView2.EditingControlShowing
        If TypeOf e.Control Is ComboBox Then
            Dim combo As ComboBox = CType(e.Control, ComboBox)
            If combo IsNot Nothing Then
                RemoveHandler combo.SelectedIndexChanged, AddressOf DataGridViewCombo_SelectedIndexChanged
                AddHandler combo.SelectedIndexChanged, AddressOf DataGridViewCombo_SelectedIndexChanged
            End If
        End If
    End Sub

    Private Sub DataGridViewCombo_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim combo As ComboBox = CType(sender, ComboBox)
        If combo.SelectedIndex > -1 Then
            Dim row As DataRow = ds.Tables("dtProjectItems").Rows.Find(DataGridView2.SelectedRows(0).Cells("ID").Value)
            RemoveHandler combo.SelectedIndexChanged, AddressOf DataGridViewCombo_SelectedindexChanged
           row("GroupID") = combo.SelectedIndex
        End If
    End Sub

Open in new window

0
Comment
Question by:Basicfarmer
2 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40596975
Are you sure that "row" is actually set to a value in line 33? In that line, if your Find operation does not return a value, then your "row" object may not be valid.
0
 

Author Closing Comment

by:Basicfarmer
ID: 40599500
Thanks for the comment Scott. That was the issue, the row was not valid...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

12 Experts available now in Live!

Get 1:1 Help Now