验证中...
Languages: ASP
Categories: 编程语言基础
Latest update 2018-12-28 08:37
gistfile1.txt
Raw Copy
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SortTest.aspx.cs" Inherits="SortTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>演示GridView的Sort方法</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class SortTest : System.Web.UI.Page
{
DataView m_DataView;
protected void Page_Load(object sender, EventArgs e)
{
//连接字符串
String sqlconn = "Data Source=WIN-20111118PND;Initial Catalog=SuperMarket;User ID=sa;Password=123456";
SqlConnection myConnection = new SqlConnection(sqlconn);
//打开数据库连接
myConnection.Open();
SqlCommand myCommand = new SqlCommand("select * from Product", myConnection);
SqlDataAdapter Adapter = new SqlDataAdapter();
Adapter.SelectCommand = myCommand;
DataSet myDs = new DataSet();
Adapter.Fill(myDs);
m_DataView = myDs.Tables[0].DefaultView;
//关闭与数据库的连接
myConnection.Close();
GridView1.DataSource = m_DataView;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
m_DataView.Sort = e.SortExpression;
GridView1.DataBind();
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_sixth 5th_float_left_close