4

Hope you can help me with this.

I'd like to filter my datagridview by using a certain keyword like a name for example. I used a data set then bind it to a data source then to my datagridview for viewing.

When I used the bindingsource.filter I can't get any result.

Here is my code:

    Dim ds As New DataSet
    Dim bs As New BindingSource

    Dim sql As String = "SELECT TOP 10 * FROM dbo.DimCustomer"
    Dim connection As New SqlConnection(sqlconnectionstring)
    Dim dataadapter As New SqlDataAdapter(sql, connection)



    connection.Open()
    ds.Clear()
    dataadapter.Fill(ds, "Customer")
    connection.Close()


    bs.DataSource = ds

    dgv1.DataSource = bs
    dgv1.DataMember = "Customer"
    bs.Filter = "FirstName = 'Jon'"
Wade73
  • 4,359
  • 3
  • 30
  • 46
helios99
  • 91
  • 1
  • 1
  • 7

3 Answers3

5

Thank you guys for the help but I got it working with the following codes below:

Dim sql As String = "select * from HumanResources.vEmployee"
Dim connection As New SqlConnection(sqlconnectionstring)
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim dsView As New DataView()

Try
  connection.Open()
  ds.Clear()
  dataadapter.Fill(ds, "test")
  dsView = ds.Tables(0).DefaultView
  bs.DataSource = dsView
  dgv1.DataSource = bs
  bs.Filter = "FirstName like 'J%'"
Catch ex As Exception
  MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
  connection.Close()
End Try
LarsTech
  • 80,625
  • 14
  • 153
  • 225
helios99
  • 91
  • 1
  • 1
  • 7
0

I believe the issue is that your bindingsource should point at a dataview and not the dataset. Here is the link to the MSDN site with a bit more explanation. Here is more information on the Dataview as well.

I haven't had time to test this, but I believe it is logically correct. Can you try this?

   dim dsView as new DataView(ds.Tables("Customer"))

   bs.DataSource = dsView
   dgv1.DataSource = bs
   dgv1.DataMember = "Customer"
   bs.Filter = "FirstName = 'Jon'"
Wade73
  • 4,359
  • 3
  • 30
  • 46
  • Hi Wade you are it should bind to a dataview but your sample still doesn't work. I tries tweaking it but got no result. I will try some more. Also if I removed the code dgv1.DataMember = "Customer" nothing will happen. – helios99 Dec 31 '12 at 17:08
  • I got it working with this: Dim dsView As New DataView() Try connection.Open() ds.Clear() dataadapter.Fill(ds, "test") dsView = ds.Tables(0).DefaultView bs.DataSource = dsView dgv1.DataSource = bs bs.Filter = "FirstName like 'J%'" – helios99 Dec 31 '12 at 17:33
  • I must have missed something, but the solution was to use the dataview of the dataset with the binding source. – Wade73 Dec 31 '12 at 17:42
  • Hi Wade thanks for the reply. I got it working now and I posted here my correct code. Thanks for the reply. – helios99 Jan 01 '13 at 14:56
0

My name is Mohamed Hosny

Your code is good and there is no error but i need from you make a couple changes First: add the table name to the dataset bs.DataSource = ds.Tables["DimCustomer"] Second: i think the filter line must come before assign the Binding Source to datagridview

bs.Filter = "FirstName = 'Jon'" dgv1.DataSource = bs

Also i think you don't need the line dgv1.DataMember = "Customer"

Try this and if this doesn't work i will give you the full code but by the c# i make it before many times.

find me on www.hcsmedia.org or www.hosysys.com

  • Hi Mohamed thank you for the response. I got my code working by some other means though. I posted here how I got it working. Thanks for the reply. – helios99 Jan 01 '13 at 14:55