如题:::
解决方案 »
- 我输入6就对6.5就报输入字符串的格式不正确的错误?
- asp.net传递中文参数显示为乱码
- 未能加载文件或程序集“Microsoft.Office.Interop.Owc11, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”或它的某一个依赖项
- 如何从js里传参调用C#后台方法返回一个字符串?
- 怎么修改DiscuzNT论坛?
- javascript高手请进(80分,分不够再加)
- 跪求二级域名问题
- 字符切割问题 ~~~~急 等
- 页面刷新后控件验证的执行?
- 大家编辑asp.net用什么工具啊
- 关于向ASPX页面提交表单的问题
- 帮忙改个小错误错误,谢谢! 急啊!
页面设计:1. 在页面上添加一个DataGrid;2. 设置DataGrid的AllowSorting属性为True;3. 设置AutogenerateColumns 属性为False;4. 添加要绑定的字段到DataGrid,并按照下表设置其属性。Sortexpression属性和数据库中数据表中的字段名保持一致。
我使用Northwind数据库中的Employees来说明这个例子。
DataTextField
Header Text Sort ExpressionEmployeeID
Employee ID
EmployeeIDLastName
Last Name
LastNameFirstName
First Name
FirstNameTitle
Title
TitleCity
City
CityCountry
Country
Country程序代码设计:1. 在页面第一次被加载时给DATAGRID添加动态属性sortexpression 和 sortdirection;2. 给指定的字段的sortexpression 和 sortdirection 赋值,在加载时初始化DataGrid的排序字段和排序方式;3. 得到dataset 和 dataview 对象。设置dataview 的sort 属性为动态属性sortexpression 和 sortdirection的连接字符串;4. 用dataview绑定datagrid;5. 当表头被点击时,触发datagrid的sortcommand事件;6. 得到sortcommand事件传递过来的sortexpression属性,并与datagrid的sortexpression属性比较;7. 如果找到相等的a) 先检查sortdirection属性;b) If sortdirection属性等于“ASC”theni. 设置sortdirection属性的值等于“DESC”c) Elsei. 设置sortdirection属性的值等于“ASC”d) End If8. 重复第3步
注意:当你运行下面的代码并点击同一列两次或者两次以上,此列的排列方式会自动根据现有的排列方式的反序排列。下面的下面的代码将给你演示怎么使用DataGrid的动态属性和DataView对象。
C# Code:
private void Page_Load(object sender, System.EventArgs e){// 在此处放置用户代码以初始化页面if(!Page.IsPostBack){if(DataGrid1.Attributes["SortExpression"] == null){DataGrid1.Attributes["SortExpression"] = "LastName";DataGrid1.Attributes["SortDirection"] = "ASC";}BindGrid();}}
private void BindGrid(){SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");conn.Open();SqlDataAdapter cmd = new SqlDataAdapter("select * from Employees",conn);DataSet ds = new DataSet();cmd.Fill(ds,"Employees");DataView dv = new DataView();dv = ds.Tables[0].DefaultView;string SortExpression = DataGrid1.Attributes["SortExpression"];string SortDirection = DataGrid1.Attributes["SortDirection"];dv.Sort = SortExpression + " " + SortDirection; DataGrid1.DataSource = dv;DataGrid1.DataBind();}
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e){string SortExpression = e.SortExpression.ToString();string SortDirection = "ASC";if(SortExpression == DataGrid1.Attributes["SortExpression"]){SortDirection = (DataGrid1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");}DataGrid1.Attributes["SortExpression"] = SortExpression;DataGrid1.Attributes["SortDirection"] = SortDirection;BindGrid();}
请按照步骤进行:
1: datagrid的属性
AllowSorting=”true”:表示启用DataGrid排序的功能
2: 在HTML里面(加在table外面)加入一个<input id="sortfield" type="hidden" value="activity_student_id" name="sortfield" runat="server">
作为排序时改变字段用
3:datagrid中的字段排序表达式和数字字段一致
就是它的SortExpression属性
4 :加入命名空间 Imports System.Data
Imports System.Data.SqlClient 5:Private Sub dg_activity_info_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dg_activity_info.SortCommand
If InStr(sortfield.Value, "desc") = 0 Then
sortfield.Value = e.SortExpression & " desc"
Else
sortfield.Value = e.SortExpression & " asc"
End If
BindGrid()
End Sub
说明: 《1》e.SortExpression等于“被按下之排序链接”的字段名称。
《2》 If InStr(sortfield.Value, "desc") = 0如果原来的排序方式原来的排序方式为“递增排序”。这时把排序方式(softfield.value)设置成”Desc”(表示递减排序)。否则设置为””,表示为“递增排序”
《3》这时softfield.value获得的就是”字段”+”排序” 接着BindGrid().
Sub bindgrid()
'每个程序员获取dataset的方式都不一样,
'总之这里你就想办法获取一个dataset数据集
Dim dtable As DataTable = dataset.Tables(0)
Dim dview As New DataView(dtable)
dview.Sort = sortfield.Value
dg.DataSource = dview
dg.DataBind()
End Sub
其实排序方法由于很多种,下面的是Doug Seven的一种方法 <%@ 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" />//利用隐藏的label
<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>
然后再绑定: