Click here to Skip to main content
15,887,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have access database called "DB" and Table Name is "Empdata" and following by details in colomn

ID	Name	Day	Amount
1	Mani	Monday	50
2	Subash	Tuesday	100
3	Mani	Tuesday	100
4	subash	Mondy	50
5	Mani	Wednesday	100
6	Subash	Wednesday	50
7	Mani	Thursday	100
8	Mani	Friday	10
9	Mani	Saturday	30
10	Subash	Saturday	20
11	Subash	Sunday	100



in vb.net i have Datagridview1 name as "dg1" and loaded with datasource
combobox1 name as "compbo"
1. i want to fill the Name in compbo
2.i want to use combobox(compbo) to filter data from Datagridview1 (dg1)
3. everything is showed correct but combobox showing same name multiple (ofcouse column having same but it should show same name in one name) for ex. Mani should show only one name if i filter that all the Mani details should show
4. And finally filter in the datagridview not working

What I have tried:

 Public Sub LD()
        ConnD()
        sql = "select ID,Name,Day,Amount from Empdata"
        cmd = New OleDbCommand(sql, conn)
        da = New OleDbDataAdapter(sql, conn)
        da.Fill(dt)
        bs.DataSource = dt
        dr = cmd.ExecuteReader()
        dg1.DataSource = bs
    End Sub



    Public Sub fillcombo()
        ConnD()
        Try
            sql = "select distinct ID,Name,Day,Amount FROM Empdata "
            cmd = New OleDbCommand(sql, conn)
            da = New OleDbDataAdapter(sql, conn)
            da.Fill(dt)
            bs.DataSource = dt
            dr = cmd.ExecuteReader()
            compbo.Items.Clear()
            dg1.DataSource = dt


            Do While dr.Read
                compbo.Items.Add(dr.GetValue(1).ToString)
                

            Loop

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        conn.Close()
        conn.Dispose()
        conn = Nothing



    End Sub
End Class
Posted
Updated 22-Feb-20 23:23pm
v7
Comments
Member 14621280 23-Feb-20 5:24am    
Datagridview not filter the value

1 solution

In your fillcombo method you are loading the DataGridView a second time, which is not necessary. Also you are reading every column and adding the name field of every record (your DISTINCT clause refers to the ID column) that you get from the database. You only need to read the name field for the ComboBox. So all you need is something like:
VB
Public Sub fillcombo()
    ConnD()
    Try
        sql = "select distinct Name FROM Empdata"
        cmd = New OleDbCommand(sql, conn)
        dr = cmd.ExecuteReader()
        compbo.Items.Clear()
        
        Do While dr.Read
            compbo.Items.Add(dr.GetValue(0).ToString)
        
        Loop
    
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    conn.Close()
    conn.Dispose()
    conn = Nothing
End Sub
 
Share this answer
 
Comments
Member 14621280 23-Feb-20 4:43am    
thank you so much but filter not happening in datagridview
Richard MacCutchan 23-Feb-20 5:41am    
Just like I suggested in my answers to your previous question, nothing will happen unless you make it happen, by executing some code. Each time you select a name from the ComboBox you will need to reload the DataGridView with the data that relates to the selected name. So you need an event handler for the ComboBox.OnSelectedItemChanged(EventArgs) Method (System.Windows.Forms) | Microsoft Docs[^] event. In that code you will need to filter the data records in your dataset and refresh the view. You need to stop and think about how you make things happen, and write out the logical steps that are required, before you write any actual code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900