Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > datagridview and combobox bound to datatable
 

datagridview and combobox bound to datatable

Hi
I have datagriview with a column  as combobox.
The combobox is bound to datatable .

i set the DisplayMember , ValuMember and DataSource properties of the combobox.
In what event an how can i access the fields of the current row of datatable.
thanks

moh hassan  Friday, March 23, 2007 1:39 PM
You can handle the editingControlShowing event to add an event handler for the combobox, here's a code sample forhow to do this:

dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

if (e.Control is ComboBox)

{

ComboBox cmb = e.Control as ComboBox;

cmb.SelectedIndexChanged -= new EventHandler(cmb_SelectedIndexChanged);

cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

}

}

void cmb_SelectedIndexChanged(object sender, EventArgs e)

{

ComboBox cmb = sender as ComboBox;

DataRowView dv = cmb.SelectedItem as DataRowView;

MessageBox.Show(dv["FieldName"].ToString());

}

Zhi-Xin Ye  Monday, March 26, 2007 7:33 AM
I had resolved the problem.
The error is due to null values , because first click of combobox , trigger the SelectedIndexChanged event with null value.
here is the resolved code with the extra blue line added:

Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim cmb As ComboBox = CType(e.Control, ComboBox)
RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
End If
End Sub

Private Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim cmb As ComboBox = CType(sender, ComboBox)
If cmb.SelectedItem IsNot Nothing Then
Dim dv As DataRowView = CType(cmb.SelectedItem, DataRowView)
MessageBox.Show(dv("UnitPrice").ToString())
end if
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

moh hassan  Friday, March 30, 2007 4:00 PM
You can handle the editingControlShowing event to add an event handler for the combobox, here's a code sample forhow to do this:

dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

if (e.Control is ComboBox)

{

ComboBox cmb = e.Control as ComboBox;

cmb.SelectedIndexChanged -= new EventHandler(cmb_SelectedIndexChanged);

cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

}

}

void cmb_SelectedIndexChanged(object sender, EventArgs e)

{

ComboBox cmb = sender as ComboBox;

DataRowView dv = cmb.SelectedItem as DataRowView;

MessageBox.Show(dv["FieldName"].ToString());

}

Zhi-Xin Ye  Monday, March 26, 2007 7:33 AM
thank you for reply
when i move to a new row , an error message appear:
"object reference not set to an instance of an object"
how can i resolve the problem
here is my code in vb.net

Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim cmb As ComboBox = CType(e.Control, ComboBox)
RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
End If
End Sub

Private Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim cmb As ComboBox = CType(sender, ComboBox)
Dim dv As DataRowView = CType(cmb.SelectedItem, DataRowView)
MessageBox.Show(dv("UnitPrice").ToString())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

moh hassan  Monday, March 26, 2007 8:00 PM
Maybe you did something wrong somewhere in other part of your code, would you mind posting the code you set up the comboboxcolumn and datagridview?
Zhi-Xin Ye  Tuesday, March 27, 2007 3:31 AM
Here is my code:
a simple form with datagrid with 2 columns: column1 : combobox , column2: textbox
form14.designer.vb


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form14
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.ProductsBindingSource = New System.Windows.Forms.BindingSource(Me.components)
Me.DsProductsBindingSource = New System.Windows.Forms.BindingSource(Me.components)
Me.DsProducts = New Pharmacy.DsProducts
Me.ProductsTableAdapter = New Pharmacy.DsProductsTableAdapters.ProductsTableAdapter
Me.Column1 = New System.Windows.Forms.DataGridViewComboBoxColumn
Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.ProductsBindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DsProductsBindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DsProducts, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGridView1
'
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2})
Me.DataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter
Me.DataGridView1.Location = New System.Drawing.Point(57, 52)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.Size = New System.Drawing.Size(240, 150)
Me.DataGridView1.TabIndex = 0
'
'ProductsBindingSource
'
Me.ProductsBindingSource.DataMember = "Products"
Me.ProductsBindingSource.DataSource = Me.DsProductsBindingSource
'
'DsProductsBindingSource
'
Me.DsProductsBindingSource.DataSource = Me.DsProducts
Me.DsProductsBindingSource.Position = 0
'
'DsProducts
'
Me.DsProducts.DataSetName = "DsProducts"
Me.DsProducts.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema
'
'ProductsTableAdapter
'
Me.ProductsTableAdapter.ClearBeforeFill = True
'
'Column1
'
Me.Column1.DataSource = Me.ProductsBindingSource
Me.Column1.DisplayMember = "ProductName"
Me.Column1.HeaderText = "Column1"
Me.Column1.Name = "Column1"
Me.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
Me.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic
Me.Column1.ValueMember = "ProductID"
'
'Column2
'
Me.Column2.HeaderText = "Column2"
Me.Column2.Name = "Column2"
'
'Form14
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(415, 266)
Me.Controls.Add(Me.DataGridView1)
Me.Name = "Form14"
Me.Text = "Form14"
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.ProductsBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DsProductsBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DsProducts, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents DsProducts As Pharmacy.DsProducts
Friend WithEvents DsProductsBindingSource As System.Windows.Forms.BindingSource
Friend WithEvents ProductsBindingSource As System.Windows.Forms.BindingSource
Friend WithEvents ProductsTableAdapter As Pharmacy.DsProductsTableAdapters.ProductsTableAdapter
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewComboBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
End Class

form14.vb

Public Class Form14
Private Sub Form14_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DsProducts.Products' table. You can move, or remove it, as needed.
Me.ProductsTableAdapter.Fill(Me.DsProducts.Products)
End Sub
'---------------------------------------------------------------------------
Public Class Form14

Private Sub Form14_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DsProducts.Products' table. You can move, or remove it, as needed.
Me.ProductsTableAdapter.Fill(Me.DsProducts.Products)

End Sub
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

If TypeOf e.Control Is ComboBox Then
Dim cmb As ComboBox = CType(e.Control, ComboBox)
RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
End If
End Sub

Private Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim cmb As ComboBox = CType(sender, ComboBox)

Dim dv As DataRowView = CType(cmb.SelectedItem, DataRowView)
MessageBox.Show(dv("UnitPrice").ToString())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub
End Class

moh hassan  Tuesday, March 27, 2007 5:45 PM
I had resolved the problem.
The error is due to null values , because first click of combobox , trigger the SelectedIndexChanged event with null value.
here is the resolved code with the extra blue line added:

Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim cmb As ComboBox = CType(e.Control, ComboBox)
RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
End If
End Sub

Private Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim cmb As ComboBox = CType(sender, ComboBox)
If cmb.SelectedItem IsNot Nothing Then
Dim dv As DataRowView = CType(cmb.SelectedItem, DataRowView)
MessageBox.Show(dv("UnitPrice").ToString())
end if
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

moh hassan  Friday, March 30, 2007 4:00 PM

You can use google to search for other answers

Custom Search

More Threads

• Bind property to selected row from datagridview
• Clever DataGrid Column Frustration
• datagrid
• Data Bound VB ComboBox Overwrites Data
• Can't Drag Table Onto DataGrid
• How to set Value of a DataGridViewComboBoxCell
• How do I change the source of a datagrid?
• Treestyleview in a datagridview?
• Need help re-positioning DataGrid
• "Formatting" the data for display