469,602 Members | 1,942 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Sort by header in dynamic datagrid

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()
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.

Nov 17 '05 #1
1 1913
Sudhir,

Are you rebinding the datgrid to the sorted dataset on postback?

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Sudhir" <sk*****@comsult.com> wrote in message
news:00****************************@phx.gbl...
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()
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.

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Kurt Schroeder | last post: by
2 posts views Thread by thomasp | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.