DataGrid控件的AllowSorting属性设为:True DataGrid容器内模板列中的LinkButton或者其它控件的CommandArgument属性设置为对应的数据库中的表字段名。 代码如下: 重新绑定 Public Sub Data_Bind(ByVal a As String) dv = 从数据库中获得DataView赋值 dv.Sort = a DataGrid1.DataSource = dv DataGrid1.DataBind() End Sub Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand Dim row, zz As String row = e.CommandName If Session("ZZ") = "DESC" Or Session("ZZ") = "" Then Session("ZZ") = "ASC" Else Session("ZZ") = "DESC" End If Dim p = row + " " + Session("ZZ") Data_Bind(p) End Sub
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<html>
<head>
<title>DataGrid Demo- Auto Generate</title>
<script language="C#" runat="server"> // Public dataTable
DataTable dataTable; // NameValue Collection for ColumnName and Sort Exp
NameValueCollection ColumnNameSortExp; void BindGrid(string SortExpression)
{
foreach(DataGridColumn column in grid1.Columns)
column.HeaderText = ColumnNameSortExp[column.SortExpression]; DataView dv = new DataView(dataTable);
dv.Sort = SortExpression;
grid1.DataSource = dv;
grid1.DataBind();
} public void Page_Load()
{
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=;";
string initialCatalog = "initial catalog=Diablo;";
string cnnString = dataSource + security + initialCatalog;
SqlConnection connection = new SqlConnection(cnnString); string strSql = "select * from role";
SqlCommand command = new SqlCommand(strSql, connection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
dataTable = new DataTable();
try
{
connection.Open();
adapter.Fill(dataTable);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
} // Add Total Column
dataTable.Columns.Add(new DataColumn("Total", typeof(Int32)));
foreach(DataRow row in dataTable.Rows)
row["Total"] = (int)row["Strength"] + (int)row["Agile"]
+ (int)row["Life"] + (int)row["Spirit"]; // Initialize the ColumnNameSortExp collections
ColumnNameSortExp = new NameValueCollection();
ColumnNameSortExp["Person"] = "角色";
ColumnNameSortExp["Strength"] = "力量";
ColumnNameSortExp["Agile"] = "敏捷";
ColumnNameSortExp["Life"] = "体力";
ColumnNameSortExp["Spirit"] = "精力";
ColumnNameSortExp["Total"] = "总计"; if(!IsPostBack)
BindGrid("");
} void Sort_Grid(Object sender, DataGridSortCommandEventArgs e)
{
string sortExpression = "";
foreach(DataGridColumn column in grid1.Columns)
{
if(column.HeaderText == ColumnNameSortExp[e.SortExpression])
{
ColumnNameSortExp[e.SortExpression] += "▲";
sortExpression = e.SortExpression + " asc";
}
else if(column.HeaderText == ColumnNameSortExp[e.SortExpression] + "▲")
{
ColumnNameSortExp[e.SortExpression] += "▼";
sortExpression = e.SortExpression + " desc";
}
else if(column.HeaderText == ColumnNameSortExp[e.SortExpression] + "▼")
{
ColumnNameSortExp[e.SortExpression] += "▲";
sortExpression = e.SortExpression + " asc";
}
}
BindGrid(sortExpression);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid id="grid1" runat="server" AutoGenerateColumns="false"
AllowSorting="true" OnSortCommand="Sort_Grid">
<HeaderStyle BackColor="lightblue" Font-Name="Arial" Font-Bold="true" />
<Columns>
<asp:BoundColumn DataField="Person" SortExpression="Person" />
<asp:BoundColumn DataField="Strength" SortExpression="Strength" />
<asp:BoundColumn DataField="Agile" SortExpression="Agile" />
<asp:BoundColumn DataField="Life" SortExpression="Life" />
<asp:BoundColumn DataField="Spirit" SortExpression="Spirit" />
<asp:BoundColumn DataField="Total" SortExpression="Total" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
DataGrid容器内模板列中的LinkButton或者其它控件的CommandArgument属性设置为对应的数据库中的表字段名。
代码如下:
重新绑定
Public Sub Data_Bind(ByVal a As String)
dv = 从数据库中获得DataView赋值
dv.Sort = a
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
Dim row, zz As String
row = e.CommandName
If Session("ZZ") = "DESC" Or Session("ZZ") = "" Then
Session("ZZ") = "ASC"
Else
Session("ZZ") = "DESC"
End If
Dim p = row + " " + Session("ZZ")
Data_Bind(p)
End Sub