Add sorting to SQLDataAdapter at runtime. How?

  • Thread starter Thread starter David Hearn
  • Start date Start date
D

David Hearn

I have a SQLDataAdapter that I have configured. Since I want to tie this to
a DataGrid and add sorting to the datagrid, I do not want to configure the
adapter with sorting. I want to be able to add the sorting at runtime
depending on the column that the user clicked on the datagrid. I am probably
missing something very simple here. Below is the code that I am using to
fill my datagrid.

Thanks in advance!

sqlMfgNumber.SelectCommand.Parameters(0).Value = "%" &
txtSearchFor.Text.Trim & "%"

sqlMfgNumber.SelectCommand.Parameters(1).Value = strOmittedNumber

sqlMfgNumber.Fill(DsMfgNumberSearch1)

If DsMfgNumberSearch1.Tables(0).Rows.Count > 0 Then

lblError.Visible = False

Datagrid1.DataSource = DsMfgNumberSearch1

Datagrid1.DataBind()

Else

lblError.Visible = True

End If
 
David Hearn said:
I have a SQLDataAdapter that I have configured.
Remember that the Adapter only moves data around, it has no bearing on the
sort order for instance. Indirectly its command properties can do this for
you, but there are better objects
Since I want to tie this to
a DataGrid and add sorting to the datagrid, I do not want to configure the
adapter with sorting. I want to be able to add the sorting at runtime
depending on the column that the user clicked on the datagrid. I am probably
missing something very simple here. Below is the code that I am using to
fill my datagrid.

Create a DataView and bind to it (DataGrid.Datasource = DataView) and you
can call the sort method of the view
http://www.knowdotnet.com/articles/dataviewsort.html You can also click on
the column header to accomplish this but if you bind to a view, you can do a
lot with filtering and sorting and specifiy multiple sort conditions.

Remember since you are in ASP.NET to call the sort before the post back is
finished. A web datagrid iss just a HTML table so the page needs to be
reconstructed to accomplish this, which is what occurs in the post back.

Also, Particularly since you are using a web app, DON't use dynamic sql
http://www.knowdotnet.com/articles/dynamisql.html

HTH,

Bill
Thanks in advance!

sqlMfgNumber.SelectCommand.Parameters(0).Value = "%" &
txtSearchFor.Text.Trim & "%"

sqlMfgNumber.SelectCommand.Parameters(1).Value = strOmittedNumber

sqlMfgNumber.Fill(DsMfgNumberSearch1)

If DsMfgNumberSearch1.Tables(0).Rows.Count > 0 Then

lblError.Visible = False

Datagrid1.DataSource = DsMfgNumberSearch1

Datagrid1.DataBind()

Else

lblError.Visible = True

End If

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
Bill,

Thanks for the reply. Using the DataView is a great idea! I will give it a
shot.

Thanks again!

David
 
Back
Top