DataGrid 控件

用到的属性 :AllowSorting,OnSortCommand

6.用Sort方法进行排序

注意:如果要设置排序数序(DESC),则在IsPostBack函数中必须指定默认的排序字段,否则会出现找不到DESC字段错误。

if (!IsPostBack)
        {
            ViewState["SortField"] = "s_Name";
            BindGrid();
        }

Sort方法的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridSort.aspx.cs" Inherits="DataGridSort" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<!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">
<script runat=server>
    OleDbConnection Myconn;
    public void Page_Load(Object src, EventArgs e)
    {
        string Myconnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + "..\\data\\score.mdb;";
        Myconn = new OleDbConnection(Myconnstring);
        Myconn.Open();

        if (!IsPostBack)
        {
            ViewState["SortField"] = "s_Name";
            BindGrid();
        }
    }
    ICollection Createtable()
    {
        string strsel = "select * from score";
        OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strsel, Myconn);
        DataSet ds=new DataSet();
        MyAdapter.Fill(ds, "score");

        return ds.Tables["score"].DefaultView;
    }
    public void BindGrid()
    {
        DataView dv = (DataView)Createtable();
        dv.Sort = (string)ViewState["SortField"]+" DESC";
        score.DataSource = dv;
        score.DataBind();
    }
    public void DataGrid_Sort(Object Sender, DataGridSortCommandEventArgs e)
    {
        ViewState["SortField"] = (string)e.SortExpression;
        BindGrid();
    }
</script>
    <title>DataGridSort</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataGrid ID="score" AutoGenerateColumns=false HeaderStyle-BackColor=ActiveCaption AlternatingItemStyle-BackColor=AliceBlue AllowSorting=true OnSortCommand="DataGrid_Sort" runat=server>
    <Columns>
    <asp:BoundColumn HeaderText="Name" DataField="s_Name" SortExpression="s_Name" ReadOnly=true/>
    <asp:BoundColumn HeaderText="Math" DataField="s_Math" SortExpression="s_Math"/>
    <asp:BoundColumn HeaderText="English" DataField="s_English" SortExpression="s_English"/>
    <asp:BoundColumn HeaderText="Chinese" DataField="s_Chinese" SortExpression="s_Chinese"/>
   
    </Columns>
    </asp:DataGrid>  
    </div>
    </form>
</body>
</html>

Repeater控件

Repeater控件的五种模板:HeaderTemplate,ItemTemplate,AlternatingItemTemplate,SeparatorTemplate,FooterTemplate

Repeater控件的代码

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!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>
<style type="text/css" class="1">
<!–
a:link { text-decoration: none}
a:active { text-decoration: none }
a:visited { text-decoration: none }
–>
</style>

<script language="c#" runat="server">
public void Page_Load(Object src,EventArgs e)
{
string Myconnstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+".\\data\\score.mdb;";
OleDbConnection Myconn=new OleDbConnection(Myconnstring);
Myconn.Open();

string strsel="select * from score";
OleDbDataAdapter MyAdapter=new OleDbDataAdapter(strsel,Myconn);
DataSet ds=new DataSet();
MyAdapter.Fill(ds,"score");
score.DataSource=ds;
score.DataMember="score";
score.DataBind();
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>DataGrid</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:Repeater id="score" runat="server">
<HeaderTemplate>
This is Header<br>
</HeaderTemplate>
<ItemTemplate>
Name:<%# DataBinder.Eval(Container.DataItem,"s_Name")%><br>
Chinese:Name:<%# DataBinder.Eval(Container.DataItem,"s_Chinese")%><br>
</ItemTemplate>
<AlternatingItemTemplate>
Name:<font color=red><%# DataBinder.Eval(Container.DataItem,"s_Name")%></font><br>
Chinese:Name:<font color=red><%# DataBinder.Eval(Container.DataItem,"s_Chinese")%></font><br>
</AlternatingItemTemplate>
<SeparatorTemplate>
<hr>
</SeparatorTemplate>
<FooterTemplate>
This is Footer
</FooterTemplate>

</asp:Repeater>
</form>
</body>
<html>

</html>

发表评论

邮箱地址不会被公开。 必填项已用*标注