Running Query on/with Binded Controls

IDE: Visual Studio 2005

Code language: Vb.Net

Hello all,

I would like to know how you would run a query on a binded source.

I have a form which has all its controls binded to an access database table.

(I took a table in "detail" view out of the data sources window and dropped it onto the form)

Now that all the controls are binded to specific columns I would like enable users to run queries on the binded data. Either specifying the requirements for one or multiple columns.

I'm assuming I will just run a select statement on the current dataset used for binding. However, if I did it that way how would I make the controls update to display the new data? Or would it do it automatically once the dataset changes.

Regardless, what do you think would be the easiest and most proficient way of achieving this?

Hope that made sense

Thanks.

[2599 byte] By [Realaxed] at [2008-1-10]
# 1

Realaxed,

According to your question on Forms binded source issues, I would like to provide you a full example to explain the related information as below:

1. Please store the SQL statement for query and connection string in the proper string as below:

Code Block

Public Const ConnectionString As String = _

"integrated security=sspi;initial catalog=pubs;data source=localhost"

Protected Const GetAllAuthorsSqlString As String = "select au_id,au_lname,au_fname,phone," + _

"address,city,state,zip,contract from authors order by au_lname,au_fname"

2. Please use DataAdapter to run the SQL statement, DataDrid control to show the query result on the form and make the data source of the DataGrid to DataSet.DataTable.

Code Block

Dim adapter As New SqlDataAdapter(GetAllAuthorsSqlString, connection)

Dim table As DataTable = dataset.Tables(0)

3. The data will be shown in the DataGrid control on the form. If you make some changes, please save the change to the database then refresh the control to see the modified results. Please write two methods, Save and Refresh as the following code snippet:

Code Block

Public Sub SaveChanges()

Dim table As DataTable = CType(datagridAuthors.DataSource, DataTable)

Dim changedRows As New ArrayList

For Each row As DataRow In table.Rows

If row.RowState <> DataRowState.Unchanged Then

changedRows.Add(row)

End If

Next

If changedRows.Count = 0 Then

Return

End If

Dim connection As New SqlConnection(ConnectionString)

connection.Open()

Dim adapter As New SqlDataAdapter(GetAllAuthorsSqlString, connection)

Dim builder As New SqlCommandBuilder(adapter)

Dim rows() As DataRow = CType(changedRows.ToArray(GetType(DataRow)), DataRow())

adapter.Update(rows)

adapter.Dispose()

connection.Close()

menuSaveChanges.Enabled = False

End Sub

Public Sub RefreshData()

Dim connection As New SqlConnection(ConnectionString)

connection.Open()

Dim adapter As New SqlDataAdapter(GetAllAuthorsSqlString, connection)

Dim dataset As New DataSet

adapter.Fill(dataset)

adapter.Dispose()

connection.Close()

Dim table As DataTable = dataset.Tables(0)

AddHandler table.ColumnChanged, New DataColumnChangeEventHandler(AddressOf ColumnChanged)

datagridAuthors.DataSource = table

End Sub

Hope that can help you.
BrunoYu-MSFT at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic IDE...