?
Solved

Need assistance with DataGridViewComboBoxCoumn

Posted on 2015-02-04
2
Medium Priority
?
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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.
Suggested Courses

764 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