datagrid sorting
-
You have to use a DataView and use the Sort property on the dataview. You can set the AllowSorting on the datagrid to true and have it set the Sort on the DataView. Here's a link to Microsoft help: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.allowsorting.aspx[^] Hope that helps. Ben
-
Hi, we can sort the data grid on column. i saw this few days back in one article. you have tp set the AllowSorting property of the DataGrid to True. This property changes all of the column header text to hyperlinks. A click on the header text hyperlink causes a post-back and a call to the OnSortCommand event handler. In datagrid control creation u have to set these properties: AllowSorting as "True" and OnsortCommand as "SortCommand_OnClick" In the OnSortCommand event handler you simply need to specify how the data should be sorted, recreate the data source and bind the data to the DataGrid. Currently creating the data source and the data binding are done in the BindData() method, so really all you need to do is specify how the data should be sorted before calling the BindData() method. In other words, redefine the SQL statement. Since the SQL statement is a page-level variable you have access to it in the OnSortCommand event handler. u can write the OnSortCommand event handler. \\ Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs) _sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression BindData() End Sub \\ hope this will give you some idea. Thanks, Rahithi Sharma
If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
-
Hi, we can sort the data grid on column. i saw this few days back in one article. you have tp set the AllowSorting property of the DataGrid to True. This property changes all of the column header text to hyperlinks. A click on the header text hyperlink causes a post-back and a call to the OnSortCommand event handler. In datagrid control creation u have to set these properties: AllowSorting as "True" and OnsortCommand as "SortCommand_OnClick" In the OnSortCommand event handler you simply need to specify how the data should be sorted, recreate the data source and bind the data to the DataGrid. Currently creating the data source and the data binding are done in the BindData() method, so really all you need to do is specify how the data should be sorted before calling the BindData() method. In other words, redefine the SQL statement. Since the SQL statement is a page-level variable you have access to it in the OnSortCommand event handler. u can write the OnSortCommand event handler. \\ Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs) _sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression BindData() End Sub \\ hope this will give you some idea. Thanks, Rahithi Sharma
If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
1.Hi pls place u r connection string in place of my con string. 2. 3.paste this code in codebehind.. imports system.data.sqlclient Dim conObj As New SqlConnection Dim adpObj As New SqlDataAdapter Dim dsObj As New DataSet Dim sort As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then bindGrid() If sort = "" Then sort = "name" End If End If End Sub Sub bindGrid() Try conObj = New SqlConnection("Data Source=B4B-2F-323-CK26;user id=sa;password=sa;Database=TEST") conObj.Open() adpObj = New SqlDataAdapter("SELECT * FROM Login", conObj) adpObj.Fill(dsObj, "Login") Dim dt As DataTable = dsObj.Tables("Login") Dim dv As DataView = New DataView(dt) dv.Sort = sort DataGrid1.DataSource = dv DataGrid1.DataBind() Catch ex As Exception Response.Write(ex.ToString()) End Try End Sub Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand sort = e.SortExpression bindGrid() End Sub End Class :laugh: