Solved

Need assistance with DataGridViewComboBoxCoumn

Posted on 2015-02-04
2
44 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get id from json Data with NewtonSoft 3 34
Advice on Xojo as a development tool over VB. 4 40
Help with AsEnumerable(), LINQ 4 22
Crystal reports vb.net 2 20
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

15 Experts available now in Live!

Get 1:1 Help Now