Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView With ComboBox
 

DataGridView With ComboBox

Hi I am building an order entry form using Windows Forms in VB.NET.
Ihave created a test/dummy application to trial and test on, and have a DataGridView with 2 columns ("Item Code", and "Description")
The "Item Code (Column1)" column is a data-bound combobox (however for my example I have just included some string values).
The second column "Description (Column2)" will retrieve the item description, based on the value selected in column1.

The below code works prefectly for what I want to achieve, however....the values are only added once focus has been lost from the row NOT when the user selects the item.
 Private Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating

        Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)

        If Not row.Cells("Column1").Value Is Nothing Then
 
            'First Item Selected
            If row.Cells("Column1").Value.ToString = "ITM01" Then
                row.Cells("Column2").Value = "Item Description 01"
                e.Cancel = False
            End If

            'Second Item Selected
            If row.Cells("Column1").Value.ToString = "ITM02" Then
                row.Cells("Column2").Value = "Item Description 02"
                e.Cancel = False
            End If
  
        End If


End Sub<br/><br/><br/>
As I mentioned above, I wish to get the item description when the user selectsthe item codefrom the comboBox. Any ideas on how to achieve this?

Thanks in advanced
kiwiDanChch  Sunday, August 02, 2009 5:15 AM
As you said , you want item description when the user selects the item code from the combobox, then why you are using Validating event, and why you set e.Cancel ?

Instead of simply use datagridview1_SelectionChanged. Refer
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged.aspx
NareshG  Monday, August 03, 2009 4:56 AM
Hi kiwiDanChch,

After I read your post, I found the problem you are facing now is how to get the value after editing not the previous one.

DataGridView.RowValidating event will fired only when you leave the current row. The changed cell value can be seen when you leave the current row.

As for the DataGridView.CellValidating event, it will fired just when you leave the current cell. Using e.FormattedValue can get the changed value.

If I misunderstood something, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 04, 2009 6:40 AM
As you said , you want item description when the user selects the item code from the combobox, then why you are using Validating event, and why you set e.Cancel ?

Instead of simply use datagridview1_SelectionChanged. Refer
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged.aspx
NareshG  Monday, August 03, 2009 4:56 AM
Hi kiwiDanChch,

After I read your post, I found the problem you are facing now is how to get the value after editing not the previous one.

DataGridView.RowValidating event will fired only when you leave the current row. The changed cell value can be seen when you leave the current row.

As for the DataGridView.CellValidating event, it will fired just when you leave the current cell. Using e.FormattedValue can get the changed value.

If I misunderstood something, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 04, 2009 6:40 AM

You can use google to search for other answers

Custom Search

More Threads

• Formatting a cell in a datagrid
• DataGridView columns change visible to true after Merge
• help
• How to add multiple controls inside GridviewCell
• DataGridViewComboBoxColumn value is not valid
• Retrieve List of Databases in Sql Server in a DropdownList in an asp.net Page ?
• Binding Navigator Problem
• ErrorProvider indicator icon not showing
• DataGridViewClick Event and how to get the clicked data to appear in a text box or multiple spots
• How to paint row with different background color if any row cell has changed?