Binding Source Filtering

I hope you help me with this.

I would like to filter my datagridview using a specific keyword like name. I used a dataset and then mapped it to a data source and then to my datagridview for viewing.

When I used bindingsource.filter , I could not 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'" 
+4
source share
3 answers

Thanks guys for the help, but I got her 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 
+4
source

I believe the problem is that your binding source should point to data, not a dataset. Here is a link to the MSDN website with a more detailed description. See below for more information on Dataview .

I did not have time to verify this, but I believe that this 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'" 
0
source

My name is mohamed hosni

Your code is good and there is no error, but I need you to make a couple of changes Firstly: add the table name to the data set bs.DataSource = ds.Tables ["DimCustomer"] Secondly: I think the filter string should appear before assigning a binding source to datagridview

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

Also I think that you do not need the line dgv1.DataMember = "Client"

Try this, and if that doesn't work, I will give you the full code, but C # I will do it many times.

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

0
source

All Articles