<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
<!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>s 13 欧拉拉</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataGrid ID ="dgclass" runat ="server" AllowSorting ="true" OnSortCommand ="sortcolumn" /> </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;
namespace WebApplication3 { public partial class _Default : System.Web.UI.Page { public string strviewstring = "number"; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { strviewString = "name"; SetData(); }
}
private void SetData() { String strconnection = "server=.;database=class;integrate security=true;"; SqlConnection objconnection = new SqlConnection(strconnection ); String strsql = "SELECT * FROM student ";
SqlDataAdapter objadapter = new SqlDataAdapter(strsql, objconnection); DataSet objdataset = new DataSet(); objadapter.Fill(objdataset, "classbiao"); DataTable classbiao = objdataset.Tables["classbiao"];
DataView dvview = new DataView(classbiao); dvview.Sort=strviewstring();
dgclass.DataSource = dvview; dgclass.DataBind(); } private void sortcolumn(object sender, DataGridSortCommandEventArgs e) { if (e.SortExpression == "phone") strviewstring = "name,phone"; else strviewstring = e.SortExpression; SetData(); } } } 有错误的 发现错误木有啊
|