Solved

Need assistance with DataGridViewComboBoxCoumn

Posted on 2015-02-04
2
50 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

789 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