filter combobox in a datgrid based on another combobox vb.net

Hi ,,

i have problem with combobox

i have two combobox and two tables
table manages (ManageID, ManageName)
table Departments (DeptID, DeptName, Dept_ManageID)
1- CboEmp_Manage
2- CboEmp_Dept

Public Class Class1
Public sub FillCombobox (ByVal cbo As Combobox, ByVal TableName As String , ByVal Col_name As String , ByVal Col_Value As String )
Try

Dim VarSql As String
VarSql = ""
VarSql = "Select " + Col_name + " , " + Col_Value + " From " + TableName + " "
Dim sda As New SqlDataAdapter(VarSql , SQLCon)
Dim ds As New Dataset ()
sda.Fill(ds)
cbo.DataSource = ds.Tables(0)
cbo.DisplayMember = Col_name
cbo.ValueMember = Col_Value
Catch
End Try
End Sub

Private Sub CboEmp_Manage_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboEmp_Manage.SelectedValueChanged

WinClass.FillCombobox (CboEmp_Dept, "Departments WHERE Dept_Manage_ID='" & CboEmp_Manage.SelecteIndex.ToString() & "' " , "Dept_Name", "Dept_ID")

End Sub

its work but its give me wrong data from table

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

VarSql = "Select " + Col_name + " , " + Col_Value + " From " + TableName + " "

Avoid using this kind of code...it is prone to sql injection...

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

please help me

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

Use a bindingsource to fill up you combo box:-)...

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

am trying to use this code but give wrong data

Dim udtsqlcmd as string
udtsqlcmd = ("select * From Departments Where Dept_manage_ID= '" & CboEmp_manage.selectedindex.tostring & "' ")

Dim dr As SQLDatadReader
Dim cmd As New SqlCommand
cmd = New SqlCommand(udtsqlcmd, Class1.sqlcon)
dr = cmd.ExecuteReader
CboEmp_Dept.Items.Clear()
Do While dr.Read
CboEmp_Dept.Items.Add(dr.Item("Dept_Name"))
Loop
dr.Close()
Class1.sqlcon.Close()

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

something wrong in my code
anyone help me

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

You are trying to put all the code together my friend..learned to sergrate the code of specific function that they are trying to do..

Dim udtsqlcmd as string
udtsqlcmd = ("select * From Departments Where Dept_manage_ID= '" & CboEmp_manage.selectedindex.tostring & "' ")

are you sure this is correct??..

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

i guess you are trying to put the ID also in the combo box??

i that what you want??..

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

ok how can i do that

i want to filter cboemp_dept based on cboemp_manage

learn me please

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

no help

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

any help here

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

no help

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

thanks

Re: filter combobox in a datgrid based on another combobox vb.net 80 80

Try this logic

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ComboBox1.Items.Add(New DataItem(1, "Male"))
        ComboBox1.Items.Add(New DataItem(2, "Female"))
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim DI As DataItem
        Dim MySex As Integer

        DI = ComboBox1.SelectedItem
        MySex = CInt(DI.Data)

        MsgBox(MySex)

    End Sub

End Class

And i created class here is it

Public Class DataItem

Private ID As Object
Private MySex As String

Public Property Data() As Object
    Get
        Data = ID
    End Get
    Set(ByVal value As Object)
        ID = value

    End Set
End Property
Public Property Sex() As String
    Get
        Sex = MySex
    End Get
    Set(ByVal value As String)
        MySex = value
    End Set
End Property

Public Overrides Function ToString() As String
    ToString = MySex
End Function

Sub New(Optional ByVal Data As Object = Nothing, Optional ByVal Text As String = "")

    MySex = Text
    ID = Data

End Sub

End Class

Actually i also found this logic here in daniWeb..

Be a part of the DaniWeb community

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