PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
filtering datagridview-VBForums
Results 1 to 6 of 6

Thread: filtering datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    3

    Question filtering datagridview

    How can filtering datagridview by using records from a combobox in vb.net
    thank you

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,440

    Re: filtering datagridview

    Populate a DataTable, bind that to a BindingSource and bind that to the DataGridView. To filter the data, set the Filter property of the BindingSource. The Filter property is just a String so it's completely up to you where that comes from. If you want to use a selection from a ComboBox, go right ahead. Before you ask for an example, you should use the keywords I've provided and see what you can find for yourself because there will be plenty of examples already out there.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    3

    Re: filtering datagridview

    I have a problem with the code:

    Name:  4444.png
Views: 48
Size:  6.4 KB

    Dim con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\KHADIJA\Documents\1الحرة\المنطقة الحرة مصراتة\المنطقة الحرة مصراتة\misurata_alhora.mdf;Integrated Security=True;Connect Timeout=30")
    Dim cmd As SqlCommand
    Dim objAdap As SqlDataAdapter
    Dim objDt As New DataTable()
    Dim dv As DataView
    Dim r As DataRow
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Select Case Comboc.SelectedItem
    '***************************************************************'
    Case "Employee_ID"
    cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID LIKE '" & TextS.Text & "%' ORDER BY Employee_ID", con)
    objAdap = New SqlDataAdapter(cmd)
    Try
    objAdap.Fill(objDt)
    If objDt.Rows.Count > 0 Then
    DataGridView1.DataSource = objDt
    objDt = New DataTable
    cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID=" & DataGridView1.Rows(0).Cells(0).Value.ToString(), con)
    objAdap = New SqlDataAdapter(cmd)
    objAdap.Fill(objDt)
    Else
    MsgBox("No Record Found.", MsgBoxStyle.OkOnly, "Personal Information")
    End If
    Catch ex As Exception
    MsgBox(ex.Message)
    Finally
    If con.State = ConnectionState.Open Then
    con.Close()
    End If
    End Try
    '***************************************************************'
    Case "Employee_Name" '
    cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_Name LIKE '" & TextS.Text & "%' ORDER BY Employee_Name", con)
    objAdap = New SqlDataAdapter(cmd)
    Try
    objAdap.Fill(objDt)
    If objDt.Rows.Count > 0 Then
    DataGridView1.DataSource = objDt
    objDt = New DataTable
    cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID=" & DataGridView1.Rows(0).Cells(0).Value.ToString(), con)
    objAdap = New SqlDataAdapter(cmd)
    objAdap.Fill(objDt)
    Else
    MsgBox("No Record Found.", MsgBoxStyle.OkOnly, "Personal Information")
    End If
    Catch ex As Exception
    MsgBox(ex.Message)
    Finally
    If con.State = ConnectionState.Open Then
    con.Close()
    End If
    End Try
    '***************************************************************'
    Case "National_Num"
    cmd = New SqlCommand("SELECT * FROM Employees WHERE National_Num LIKE '" & TextS.Text & "%' ORDER BY National_Num", con)
    objAdap = New SqlDataAdapter(cmd)
    Try
    objAdap.Fill(objDt)
    If objDt.Rows.Count > 0 Then
    DataGridView1.DataSource = objDt
    objDt = New DataTable
    cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID=" & DataGridView1.Rows(0).Cells(0).Value.ToString(), con)
    objAdap = New SqlDataAdapter(cmd)
    objAdap.Fill(objDt)
    Else
    MsgBox("No Record Found.", MsgBoxStyle.OkOnly, "Personal Information")
    End If
    Catch ex As Exception
    MsgBox(ex.Message)
    Finally
    If con.State = ConnectionState.Open Then
    con.Close()
    End If
    End Try
    End Select
    End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,440

    Re: filtering datagridview

    Please format code for readability.
    vb.net Code:
    1. Dim con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\KHADIJA\Documents\1الحرة\المنطقة الحرة مصراتة\المنطقة الحرة مصراتة\misurata_alhora.mdf;Integrated Security=True;Connect Timeout=30")
    2.     Dim cmd As SqlCommand
    3.     Dim objAdap As SqlDataAdapter
    4.     Dim objDt As New DataTable()
    5.     Dim dv As DataView
    6.     Dim r As DataRow
    7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.         Select Case Comboc.SelectedItem
    9. '***************************************************************'
    10.             Case "Employee_ID"
    11.                 cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID LIKE '" & TextS.Text & "%' ORDER BY Employee_ID", con)
    12.                 objAdap = New SqlDataAdapter(cmd)
    13.                 Try
    14.                     objAdap.Fill(objDt)
    15.                     If objDt.Rows.Count > 0 Then
    16.                         DataGridView1.DataSource = objDt
    17.                         objDt = New DataTable
    18.                         cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID=" & DataGridView1.Rows(0).Cells(0).Value.ToString(), con)
    19.                         objAdap = New SqlDataAdapter(cmd)
    20.                         objAdap.Fill(objDt)
    21.                     Else
    22.                         MsgBox("No Record Found.", MsgBoxStyle.OkOnly, "Personal Information")
    23.                     End If
    24.                 Catch ex As Exception
    25.                     MsgBox(ex.Message)
    26.                 Finally
    27.                     If con.State = ConnectionState.Open Then
    28.                         con.Close()
    29.                     End If
    30.                 End Try
    31. '***************************************************************'
    32.             Case "Employee_Name"               '              
    33.                 cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_Name LIKE '" & TextS.Text & "%' ORDER BY Employee_Name", con)
    34.                 objAdap = New SqlDataAdapter(cmd)
    35.                 Try
    36.                     objAdap.Fill(objDt)
    37.                     If objDt.Rows.Count > 0 Then
    38.                         DataGridView1.DataSource = objDt
    39.                         objDt = New DataTable
    40.                         cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID=" & DataGridView1.Rows(0).Cells(0).Value.ToString(), con)
    41.                         objAdap = New SqlDataAdapter(cmd)
    42.                         objAdap.Fill(objDt)
    43.                     Else
    44.                         MsgBox("No Record Found.", MsgBoxStyle.OkOnly, "Personal Information")
    45.                     End If
    46.                 Catch ex As Exception
    47.                     MsgBox(ex.Message)
    48.                 Finally
    49.                     If con.State = ConnectionState.Open Then
    50.                         con.Close()
    51.                     End If
    52.                 End Try
    53. '***************************************************************'
    54.             Case "National_Num"
    55.                 cmd = New SqlCommand("SELECT * FROM Employees WHERE National_Num LIKE '" & TextS.Text & "%' ORDER BY National_Num", con)
    56.                 objAdap = New SqlDataAdapter(cmd)
    57.                 Try
    58.                     objAdap.Fill(objDt)
    59.                     If objDt.Rows.Count > 0 Then
    60.                         DataGridView1.DataSource = objDt
    61.                         objDt = New DataTable
    62.                         cmd = New SqlCommand("SELECT * FROM Employees WHERE Employee_ID=" & DataGridView1.Rows(0).Cells(0).Value.ToString(), con)
    63.                         objAdap = New SqlDataAdapter(cmd)
    64.                         objAdap.Fill(objDt)
    65.                     Else
    66.                         MsgBox("No Record Found.", MsgBoxStyle.OkOnly, "Personal Information")
    67.                     End If
    68.                 Catch ex As Exception
    69.                     MsgBox(ex.Message)
    70.                 Finally
    71.                     If con.State = ConnectionState.Open Then
    72.                         con.Close()
    73.                     End If
    74.                 End Try
    75.         End Select
    76.     End Sub

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,440

    Re: filtering datagridview

    That error message is completely unrelated to filtering a DataGridView and should thus you should ask about it in a separate thread. The error message tells you what the problem is. Obviously the file is not on a UNC share so the issue must be one of the other two and there's not much we can tell you about either, based on the information we have. That said, sometimes SQL Server Express databases can seem to get stuck, but that's usually only if they are in the project output folder. Not sure that's the case here. Try cleaning your project output and rebuilding or possibly even restarting Windows to ensure no data files are attached.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    3

    Re: filtering datagridview

    thank you .. i did it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width