How DataGrid has built-in Column Sorting functionality
Getting and Displaying the Data
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DotNetJunkies.com - Column Sorting in the DataGrid</title>
<script runat="server" language="VB">
Protected _sqlStmt As String = "SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
DimconStringAsString="server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myDataSet As New DataSet
Dim myDataAdapter As New SqlDataAdapter(_sqlStmt, conString)
myDataAdapter.Fill(myDataSet, "Customers")
myDataGrid.DataSource = myDataSet.Tables("Customers")
myDataGrid.DataBind()
End Sub
</script>
<style>
.DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>
</head>
<body>
<form runat="server" method="post">
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
/>
</form>
</body>
</html>
The Revised DataGrid Properties
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
AllowSorting="True"
OnSortCommand="SortCommand_OnClick"
/>
The SortCommand_OnClick() Event Handler
Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
_sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression
BindData()
End Sub
Column Sorting and Paging Together
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>ASPNextGen.com - Column Sorting in the DataGrid</title>
<script runat="server" language="VB">
Protected _sqlStmt As String = "SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
SQLStatement.Text = _sqlStmt
BindData()
End If
End Sub
Sub BindData()
Dim myDataSet As New DataSet
DimConStringAsString="server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myDataAdapter As New SqlDataAdapter(SQLStatement.Text, ConString)
myDataAdapter.Fill(myDataSet, "Customers")
myDataGrid.DataSource = myDataSet.Tables("Customers")
myDataGrid.DataBind()
End Sub
Sub PageIndexChanged_OnClick(Source As Object, E As DataGridPageChangedEventArgs)
myDataGrid.CurrentPageIndex = E.NewPageIndex
BindData()
End Sub
Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
SQLStatement.Text = _sqlStmt & " ORDER BY " & E.SortExpression
BindData()
End Sub
</script>
<style>
.DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>
</head>
<body>
<form runat="server" method="post">
<asp:Label id="SQLStatement" runat="server" Visible="False" />
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
AllowSorting="True"
OnSortCommand="SortCommand_OnClick"
AllowPaging="True"
OnPageIndexChanged="PageIndexChanged_OnClick"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
/>
</form>
</body>
</html>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DotNetJunkies.com - Column Sorting in the DataGrid</title>
<script runat="server" language="VB">
Protected _sqlStmt As String = "SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
DimconStringAsString="server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myDataSet As New DataSet
Dim myDataAdapter As New SqlDataAdapter(_sqlStmt, conString)
myDataAdapter.Fill(myDataSet, "Customers")
myDataGrid.DataSource = myDataSet.Tables("Customers")
myDataGrid.DataBind()
End Sub
</script>
<style>
.DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>
</head>
<body>
<form runat="server" method="post">
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
/>
</form>
</body>
</html>
The Revised DataGrid Properties
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
AllowSorting="True"
OnSortCommand="SortCommand_OnClick"
/>
The SortCommand_OnClick() Event Handler
Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
_sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression
BindData()
End Sub
Column Sorting and Paging Together
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>ASPNextGen.com - Column Sorting in the DataGrid</title>
<script runat="server" language="VB">
Protected _sqlStmt As String = "SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
SQLStatement.Text = _sqlStmt
BindData()
End If
End Sub
Sub BindData()
Dim myDataSet As New DataSet
DimConStringAsString="server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myDataAdapter As New SqlDataAdapter(SQLStatement.Text, ConString)
myDataAdapter.Fill(myDataSet, "Customers")
myDataGrid.DataSource = myDataSet.Tables("Customers")
myDataGrid.DataBind()
End Sub
Sub PageIndexChanged_OnClick(Source As Object, E As DataGridPageChangedEventArgs)
myDataGrid.CurrentPageIndex = E.NewPageIndex
BindData()
End Sub
Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
SQLStatement.Text = _sqlStmt & " ORDER BY " & E.SortExpression
BindData()
End Sub
</script>
<style>
.DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>
</head>
<body>
<form runat="server" method="post">
<asp:Label id="SQLStatement" runat="server" Visible="False" />
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
AllowSorting="True"
OnSortCommand="SortCommand_OnClick"
AllowPaging="True"
OnPageIndexChanged="PageIndexChanged_OnClick"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
/>
</form>
</body>
</html>
Labels: DataGrid

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home