Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching A Gridview With A DataTable Datasource

I have a gridview that gets its data from a webservice.

This comes into the application in a dataset.

   Me.GvStreets.DataSource = TheWebServiceSearch.AddressDataTable
   Me.GvStreets.DataBind()

Once in the grid view how do I search the contents of this dataset.

Do I have to add it to some sort of datasource control like an XML datasource?

Thanks

What I ended up doing was this...

  Dim StreetDataTable As DataTable = Session("StreetData")
   Dim Name As String = StreetDataTable.Columns(0).ColumnName

   Dim FilteredResults As New DataTable
   FilteredResults = StreetDataTable.Clone()

   Dim DataRows() As DataRow
   DataRows = StreetDataTable.Select("street LIKE '%" & Me.txtStreet.Text & _


                         "%'", "Street ASC")
    Dim i As Integer

    For i = 0 To DataRows.GetUpperBound(0)

        FilteredResults.ImportRow(DataRows(i))

    Next i

    Me.GvStreets.DataSource = FilteredResults
    Me.GvStreets.DataBind()

I had to get the results and clone the data table to get the schema. Then I did the select from the original datatable. I looped through the results and added them to the cloned data table.

like image 317
Paul Avatar asked May 10 '26 15:05

Paul


1 Answers

Normally you would search your data source directly, so in your case since TheWebServiceSearch.AddressDataTable is a DataTable, you can do the following:

DataTable data = TheWebServiceSearch.AddressDataTable;
DataRow[] foundRows = data.Select("city = 'NY'", "zip ASC");

You can check out the complete list of DataTable.Select overloads here


Oh okay, now I see what you need. I thought you wanted something else. Anyways for your purpose you should use DataView object (which is also bindable). Here's an example:

Dim StreetDataTable As DataTable = Session("StreetData")
Dim Name As String = StreetDataTable.Columns(0).ColumnName
StreetDataTable.DefaultView.RowFilter = "street LIKE '%" & Me.txtStreet.Text & "%'"
StreetDataTable.DefaultView.Sort = "Street ASC"

Me.GvStreets.DataSource = StreetDataTable.DefaultView
Me.GvStreets.DataBind()

Take a look at complete specification of DataView.

like image 59
niaher Avatar answered May 13 '26 13:05

niaher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!