Datagrid Binding

G

Greg

I have a page with two datagrids that shows the results from a single SQL
statement. I would like to have one of the datagrids only show specific
fields based on what the user chooses from a list box. For example, the
user could choose "List A" from the list box and have only related fields
(that I define in code behind) show in the grid.

How can I do this?


TIA
 
P

Paul Hobbs

Hi Greg,

One way is to bind the datagrid to a dataview (rather than a datatable or dataset). Then when your user selects an item from the listbox, you simply filter the dataview based on the selection, and rebind the datagrid.

Sample code:

Private Sub lstArtistsFilter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstArtistsFilter.SelectedIndexChanged
Dim strArtist As String = lstArtistsFilter.SelectedItem.Text
'refill datatable
daSelectTrackList.Fill(dtTrackList)
'determine sort order
If Not viewstate("sortorder") Is Nothing Then
dvTrackList.Sort = viewstate("sortorder")
Else
dvTrackList.Sort = strSortOrder
End If
'filter the data in the dataview
If lstArtistsFilter.SelectedItem.Value = -1 Then
dvTrackList.RowFilter = "ArtistID <> -1"
Else
dvTrackList.RowFilter = "ArtistName = '" & strArtist & "'"
End If
grdTrackList.DataSource = dvTrackList
grdTrackList.DataBind()
End Sub

Hope this helps.

Paul Hobbs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top