Hi All,
I need code which support vb.net 2005
Problem:
While clicking the items in DataGridViewComboBox which inserted in DataGridView( Grid control in vb.net 2005). i want to populate the records in the grid that related to the item which i selected in combo box.
I will explain with example:
I have grid control named GridCustomer. My grid have three columns which are customer ID, Customer Name, and Address. On the first column i created one comboboxcontain Customer ID(CmbCustomerID)which comes along with DataGridView. So while selecting this CmbCustomerID i want to display corresponding records in the grid. The data which are coming in the combo box is from table.
Sample Code
I am attaching sample code, that will generate grid with combobox with data. and grid directly populating data from table not according to the selection from combo.if anybody can help how to write code, if necessary i will send complete project source code. so it is easy to under stand.
My contact Id:
JoshyPJoseph@hotmail.com
kannadi@gmail.com( I am online ,7AM-2PM-Kuwait Time)
--------------------------------------------------------------------------------------
mports
System
Imports
System.Data
Imports
System.Data.SqlClient
Imports
System.Windows.Forms
'new
Imports
System.Collections.Generic
'Public Enum Title
' King
' Sir
'End Enum
Public
Class Form1
Inherits System.Windows.Forms.Form
Public comb As New DataGridViewComboBoxColumn()
Public Const item_code_label As String = "item_code"
Public Const tbl_item_label As String = "tbl_item"
Private dataGridView1 As New DataGridView()
Private bindingSource1 As New BindingSource()
Private dataAdapter As New SqlDataAdapter()
Private WithEvents reloadButton As New Button()
Private WithEvents submitButton As New Button()
Dim con As New SqlConnection(_shared.constr)
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(
New Form1())
End Sub
Private Sub SetupForm()
AutoSize =
True
End Sub
'new
Private Sub setupgridcombo()
' Initialize the DataGridView.
' dataGridView1.AutoGenerateColumns = False
dataGridView1.AutoSize =
True
dataGridView1.Columns.Add(CreateComboBoxWithEnums())
' Initialize the form.
Controls.Add(dataGridView1)
Me.AutoSize = True
Me.Text = "DataGridView object binding demo"
End Sub
' Bind combobox object data grid view control
Private Function CreateComboBoxWithEnums() As DataGridViewComboBoxColumn
Dim ds As New DataSet
Dim dv As New DataView
Dim da As New SqlDataAdapter
Dim con As New SqlConnection(_shared.constr)
con.Open()
Try
ds.Clear()
da =
New SqlDataAdapter("SELECT [item_code] FROM [GPMC].[dbo].[tbl_item]", con)
da.Fill(ds, tbl_item_label)
dv =
New DataView(ds.Tables(tbl_item_label), "", item_code_label, DataViewRowState.OriginalRows)
Catch oEx As Exception
MessageBox.Show(oEx.Message)
End Try
If Not con Is Nothing Then
End If
comb.DataSource = dv
comb.ValueMember =
"item_code"
comb.DisplayMember =
"item_code"
comb.DataPropertyName =
"Title"
comb.Name =
"Title"
Return comb
da.Dispose()
da =
Nothing
ds.Dispose()
ds =
Nothing
dv.Dispose()
dv =
Nothing
con.Close()
con.Dispose()
End Function
' Initialize the form.
Public Sub New()
setupgridcombo()
SetupForm()
Me.dataGridView1.Dock = DockStyle.Fill
Me.reloadButton.Text = "reload"
Me.submitButton.Text = "submit"
Dim panel As New FlowLayoutPanel()
panel.Dock = DockStyle.Top
panel.AutoSize =
True
panel.Controls.AddRange(
New Control() {Me.reloadButton, Me.submitButton})
Me.Controls.AddRange(New Control() {Me.dataGridView1, panel})
Me.Text = "DataGridView databinding and updating demo"
End Sub
Private Sub reloadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles reloadButton.Click
' Reload the data from the database.
GetData(
Me.dataAdapter.SelectCommand.CommandText)
End Sub
Private Sub submitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles submitButton.Click
' Update the database with the user's changes.
Me.dataAdapter.Update(CType(Me.bindingSource1.DataSource, DataTable))
End Sub
Private Sub GetData(ByVal selectCommand As String)
Try
' Create a new data adapter based on the specified query.
Me.dataAdapter = New SqlDataAdapter(selectCommand, con)
' Create a command builder to generate SQL update, insert, and
' delete commands based on selectCommand. These are used to
' update the database.
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
' Populate a new data table and bind it to the BindingSource.
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
' Resize the DataGridView columns to fit the newly loaded content.
Me.dataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
Catch ex As SqlException
MessageBox.Show(
"To run this example, replace the value of the " + _
"connectionString variable with a connection string that is " + _
"valid for your system.")
End Try
End Sub
Private Sub comboselect()
Dim GridItem As DataGridViewCell
GridItem = dataGridView1.Item(0, dataGridView1.CurrentCell.RowIndex)
Dim str As String
str = GridItem.Value.ToString.Trim
MsgBox(str)
Me.dataGridView1.DataSource = Me.bindingSource1
GetData(
"select * from tbl_item WHERE item_code = '" & str & "'")
End Sub
End
Class