hi...
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
reply

Or contact me 9049776416

e-mail me prajot_03@yahoo.com

Recommended Answers

All 13 Replies

Which part is giving you the trouble?
There are many issues mentioned in your posting.

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.

plz give me ur contact no....

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...

Image_2Image_3

This post has no text-based content.

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

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()
            InitializeComponent()

            '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)
                    da.Fill(table)
                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
                    da.Fill(table)
                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)
con.open()
dim dt as new datatable()
da.fill(dt)
comboproduct.datasource= dt
comboproduct.displayfield="productname"
con.close()

hope this will give you an idea .

Best Regards

M.Waqas Aslam

thnxxxxxxx artemax22

hey.. i'm artemix not artemax.. zzzzz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.