Sorting by header in a dynamic datagrid

S

Sudhir

I have created a dynamic datagrid and would like to sort
the grid when I click the column name in the header.
-----------------------------------------------
Private Sub btnSearch_Click(ByVal sender As
System.Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
BindGrid("")
End Sub

Private Sub BindGrid(ByVal Sortfield As String)
Dim strwhere1 As String = txtSrchform1.Text
Dim strwhere2 As String = txtSrchform2.Text
Dim Pid As Integer = CInt(Request.Cookies
("PageID").Value)
Dim dview As DataView
Dim ds As DataSet

ds = searchresults(Pid, "%" & strwhere1 & "%", "%"
& strwhere2 & "%", "")
dview = ds.Tables("Codes").DefaultView
dview.Sort = Sortfield

Dim path As String = PagePath(Pid)
Response.Write(path)

dg1.ID = "SrchResults"
dg1.AutoGenerateColumns = True
dg1.AllowSorting = True
dg1.DataSource = dview
dg1.DataKeyField = ds.Tables("Codes").Columns
(0).ColumnName

Dim hc1 As New HyperLinkColumn()
hc1.Text = "Select"
hc1.DataNavigateUrlField = ds.Tables
("Codes").Columns(0).ColumnName
hc1.DataNavigateUrlFormatString = path & "?ID={0}"
dg1.Columns.Add(hc1)
GridPlaceHolder.Controls.Add(dg1)

AddHandler dg1.ItemDataBound, AddressOf Hide_Column
AddHandler dg1.SortCommand, AddressOf
dg1_SortCommand
dg1.DataBind()

dgSrchResults.DataSource = dview
dgSrchResults.DataBind()
dgSrchResults.AutoGenerateColumns = True
dgSrchResults.DataKeyField = ds.Tables
("Codes").Columns(0).ColumnName

End Sub

Sub dg1_SortCommand(ByVal source As Object, ByVal e As
DataGridSortCommandEventArgs)
BindGrid(e.SortExpression)
End Sub

---------------------------------------------------

When I click the column name on the header, the grid
disappears. I do not understand where I have gone wrong.

Any help is appreciated.

Thanks,
Sudhir.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top