void BindGrid() { // create the connection, DataAdapter and DataSet string connString = "server=(local);database=Northwind;uid=sa;pwd=;"; string sql = "SELECT * FROM Employees"; SqlConnection conn = new SqlConnection(connString); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet();
// fill the dataset and retrieve the default view da.Fill(ds, "Employees"); DataView dv = ds.Tables["Employees"].DefaultView;
// sort the view according to the value stored in the ViewState, in any if (ViewState["SortExpression"] != null) dv.Sort = ViewState["SortExpression"].ToString(); Datagrid1.DataSource = dv; Datagrid1.DataBind(); }
{
dglook.CurrentPageIndex=0;
ViewState["sortExpre"]=e.SortExpression;
databinds(e.SortExpression);
}
<asp:datagrid id="dglook" runat="server" Width="512px" ShowFooter="True" AllowSorting="True" AllowPaging="True"
PageSize="4" CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CCCCCC" AutoGenerateColumns="False"
Height="216px">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:CheckBox ID="chkdel" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="id" SortExpression="sortid" HeaderText="编号"></asp:BoundColumn>
<asp:BoundColumn DataField="name" SortExpression="sortname" HeaderText="姓名"></asp:BoundColumn>
<asp:BoundColumn DataField="sex" SortExpression="sortsex" HeaderText="性别"></asp:BoundColumn>
<asp:BoundColumn DataField="mz" SortExpression="sortmz" HeaderText="民族"></asp:BoundColumn>
<asp:BoundColumn DataField="csrq" SortExpression="csrq" HeaderText="出生日期"></asp:BoundColumn>
<asp:BoundColumn DataField="card" HeaderText="身份证号"></asp:BoundColumn>
<asp:HyperLinkColumn Text="管理" NavigateUrl="update.aspx"></asp:HyperLinkColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:datagrid><asp:button id="selall" Text="全部选中" Runat="server"></asp:button><asp:button id="delate" Text="删除选中" Runat="server"></asp:button>
<P></P>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form method=post runat="server">
<asp:DataGrid runat="server" ID="Datagrid1"
AutoGenerateColumns="False"
Width="100%"
AllowSorting="true"
OnSortCommand="Datagrid1_Sort"
HeaderStyle-Font-Size="10"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="Red"
HeaderStyle-BackColor="Yellow"
HeaderStyle-BorderColor="Red"
HeaderStyle-BorderWidth="5"
FooterStyle-BorderColor="Red"
FooterStyle-BorderWidth="5"
ItemStyle-BackColor="LightCyan"
ItemStyle-ForeColor="DarkBlue"
AlternatingItemStyle-BackColor="LightYellow"
AlternatingItemStyle-ForeColor="Maroon"
AlternatingItemStyle-Font-Italic="true">
<Columns>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID" SortExpression="EmployeeID"/>
<asp:BoundColumn HeaderText="Title" ItemStyle-Width="50px" DataField="TitleOfCourtesy" SortExpression="TitleOfCourtesy"/>
<asp:BoundColumn HeaderText="Last Name" ItemStyle-Width="150px" DataField="LastName" SortExpression="LastName"/>
<asp:BoundColumn HeaderText="First Name" DataField="FirstName" SortExpression="FirstName"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html><script runat="server" language="C#">
void Page_Load()
{
if (!Page.IsPostBack)
BindGrid();
}
void BindGrid()
{
// create the connection, DataAdapter and DataSet
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = "SELECT * FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
// fill the dataset and retrieve the default view
da.Fill(ds, "Employees");
DataView dv = ds.Tables["Employees"].DefaultView;
// sort the view according to the value stored in the ViewState, in any
if (ViewState["SortExpression"] != null)
dv.Sort = ViewState["SortExpression"].ToString(); Datagrid1.DataSource = dv;
Datagrid1.DataBind();
}
void Datagrid1_Sort(object sender, DataGridSortCommandEventArgs e)
{
ViewState["SortExpression"] = e.SortExpression;
BindGrid();
}
</script>