Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filter Datatable and Bind to Datagrid

Status
Not open for further replies.

faithful1

Programmer
Sep 27, 2002
49
US
Hello...i am trying to filter a datatable and rebind the results to my datagrid.

My datagrid columns display displayed like so:

<datagrid autogeneratecolumns=false>
<asp:templatecolumn>
<itemtemplate>
<%#Databinder.EvalContainer.Dataitem,&quot;varLastName&quot;)%>
</itemtemplate>
</asp:templatecolumn>
</datagrid>

When I run my initial query, I create a datatable, cache it and bind to the datagrid:

Dim Search As DataTable = New DataTable()
Dim dr As DataRow
Dim i As Integer
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
adapter.SelectCommand = myCmd
adapter.Fill(Search)
Session(&quot;Results&quot;) = Search
dgSearch.DataSource = Search
dgSearch.DataBind()

I want to take this datatable out of cache, filter it, insert the results into a new datatable and bind to the datagrid again.
I tried the following, but get an error 'System.Web.HttpException: DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name varLastName.
Dim Filter, Sort As String
Dim tempdt As DataTable
Dim filterdt As DataTable
Dim drs() As DataRow
Dim dr As DataRow

Filter = &quot; intEmployeeID > 0 &quot;
Sort = &quot; intEmployeeID DESC&quot;
tempdt = CType(Session(&quot;Results&quot;), DataTable)
drs = tempdt.Select(Filter, Sort)
filterdt = New DataTable()
Dim row As DataRow
For Each row In drs
filterdt.ImportRow(row)
Next
Dim dv As DataView = filterdt.DefaultView
dgSearch.DataSource = dv
dgSearch.DataBind()


Thanks in advance!
 
I found the solution for those of you who are interested. My filter was the id of a button clicked:
Dim i As Integer
Dim ds As DataSet
Dim tempdt As DataTable
Dim drs() As DataRow
Dim dr As DataRow
Dim dv As DataView
Dim alpha As String = CType(sender, Button).ID
Session(&quot;alpha&quot;) = alpha
tempdt = CType(Session(&quot;Results&quot;), DataTable)
tempdt.DefaultView.RowFilter = &quot; varLastName LIKE '&quot; + Session(&quot;alpha&quot;) + &quot;%'&quot;
tempdt.DefaultView.Sort = &quot; varLastName ASC&quot;
dgSearch.DataSource = tempdt.DefaultView
dgSearch.DataBind()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top