I am having a problem in vb.net

I have a data gride view ,in which 2 drop down list (coloumn) are there

  1. select category
  2. select item name

there are 2 category brandy and whisky

if i select brandy from drop down list from datagrideview

it should populate the other coloumn(item name) with all the brandy names from the database(mysql)

plz help

Or contact me 9049776416

e-mail me prajot_03@yahoo.com

Edited by prajot_03

4 Years
Discussion Span
Last Post by artemix22

create new select query and use a WHERE clasue as an item from 1st drop down list (catgory). Then fill datatable, and bind it to 2nd drop down list.


No. We will discuss here about the issue.

So tell me, how you populate comboBox1? From database? Are the items databound to some datasource like datatable?
How do you intend to get data for populating comboBox2? Bind them to the comboBox2?

Tell us more... we would be more then glad to help you out...


category and item name both are combo box and are populated from database(mysql)

Attachments Image_2.png 93.28 KB Image_3.png 111.89 KB

I did the whole code for you, since I see its pointeless to do long explanations what to do.
Here it is (btw, code work you know):

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient

Namespace Apr11_1
    Public Partial Class Form1
        Inherits Form
        Private comboBox2 As DataGridViewComboBoxColumn
        Public Sub New()

            'creating 2 comboBox columns:
            Dim comboBox1 As DataGridViewComboBoxColumn = CreatingComboBox("column1", "Category")
            comboBox2 = CreatingComboBox("column2", "Item name")
            dataGridView1.Columns.Insert(0, comboBox1)
            dataGridView1.Columns.Insert(1, comboBox2)

            'and creating other columns (textboxes):
            'you wll add more (quantity, rate,...)
            dataGridView1.Columns.Add("column3", "Column 3")

            'bind data to 1st combo:
            Dim table As DataTable = GetData_ForComboBox1()
            comboBox1.DataSource = table.DefaultView
            comboBox1.DisplayMember = "Column1"
                'comboBox1.ValueMember = "CategoryID";
            comboBox1.DataPropertyName = "Column1"
        End Sub

        Private Function CreatingComboBox(name As String, headerText As String) As DataGridViewComboBoxColumn
            Dim cmbcolumn As New DataGridViewComboBoxColumn()
            If True Then
                cmbcolumn.Name = name
                cmbcolumn.HeaderText = headerText
                If name = "column1" Then
                    'creating event for comboBox1:
                    dataGridView1.EditingControlShowing += New DataGridViewEditingControlShowingEventHandler(AddressOf dataGridView1_EditingControlShowing)
                End If
            End If
            Return cmbcolumn
        End Function

        Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
            Dim combo As ComboBox = TryCast(e.Control, ComboBox)
            If combo IsNot Nothing Then
                ' Remove an existing event-handler, if present, to avoid 
                ' adding multiple handlers when the editing control is reused.
                RemoveHandler combo.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
                ' Add the event handler. 
                AddHandler combo.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
            End If
        End Sub

        Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim cb As ComboBox = TryCast(sender, ComboBox)

            'get the data from bind cell of comboBox:
            Dim view As DataRowView = TryCast(cb.SelectedItem, DataRowView)
            If view IsNot Nothing Then
                Dim item As String = view("CategoryName").ToString()
                'specify the column name
                Dim table2 As DataTable = GetData_ForComboBox2(item)
                comboBox2.DataSource = table2.DefaultView
                comboBox2.DisplayMember = "ItemName"
            End If
        End Sub

        Private Function GetData_ForComboBox1() As DataTable
            Dim table As New DataTable()
            Using conn As New SqlConnection("connString")
                Using da As New SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", conn)
                End Using
            End Using
            Return table
        End Function

        Private Function GetData_ForComboBox2(item As String) As DataTable
            Dim table As New DataTable()
            Using conn As New SqlConnection("connString")
                Dim query As String = "SELECT ItemID, ItemName FROM Items WHERE CategoryName = @parameter1"
                Using da As New SqlDataAdapter(query, conn)
                    da.SelectCommand.Parameters.Add("@parameter1", SqlDbType.VarChar, 50).Value = item
                End Using
            End Using
            Return table
        End Function
    End Class
End Namespace

giving problem on this line
DataGridView1.EditingControlShowing += New DataGridViewEditingControlShowingEventHandler(AddressOf dataGridView1_EditingControlShowing)

Error is

[Error 4 'Public Event EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\Users\trilok\Desktop\pro_leberty\pro_leberty\supplier_update.vb 42 21 pro_leberty]


this may help you Invocation of Row.Update() method initiates verification of all the above conditions for a single row among ordered rows in a grid. This process is much more efficient than working with unsorted data, especially during sorting. Invocation of Row.Update() in the event-driven model occurs regularly upon notification from the INotifyPropertyChanged interface. from dapfor.com/en/net-suite/net-grid/features/data-binding-and-adding-objects


i think you have to use a simple code which you used to populate your combobox what you have to do it to use a query with where clause and use that query to populate the combo call that query at your first combo where you select the brandy. you code may be look like this.
dim con as new sqlconnnection("your connection string")
dim da as new sqldataadapter("select productname from table where brandy="&cmbBrandy.text,con)
dim dt as new datatable()
comboproduct.datasource= dt

hope this will give you an idea .

Best Regards

M.Waqas Aslam

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.