国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

asp.net 2.0下一個標準GRIDVIEW功能的實現(xiàn)(不用datasource控件)

2019-11-18 17:05:43
字體:
來源:轉載
供稿:網(wǎng)友

asp.net 2.0下,gridview是十分方便的了,加一個DATASOURCE系列的控件的話,就可以馬上和gridview綁定,十分方便。但其實也可以
使用datatable或者dataview的,這個時候就不是用datasource系列控件了。下面講下如何在asp.net 2.0下,實現(xiàn)gridview控件的翻頁,各列排序,
編輯的功能。
    首先,我們讀取的是northwind數(shù)據(jù)庫中的employee表。放置一個gridview后,添加幾個綁定的列,代碼如下
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
            Width="100%" DataKeyNames="EmployeeID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" PageSize="3" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand">
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="employeeid" HeaderText="Employee ID" ReadOnly="True" />
                <asp:BoundField DataField="firstname" HeaderText="First Name" SortEx                <asp:BoundField DataField="lastname" HeaderText="Last Name" SortExpression="lastname" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

首先,我們要實現(xiàn)分頁,把AllowPaging設置為true,并設置每頁的分頁條數(shù),最后在codebehind中寫入
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
  為了實現(xiàn)每列都可以自動點擊排序,可以設置allowsorting=true,然后設置OnSorting="GridView1_Sorting",其中gridview_sorting
代碼為
  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortexpression"] = e.SortExpression;

        if (ViewState["sortdirection"] == null)
        {
            ViewState["sortdirection"] = "asc";
        }
        else
        {
            if (ViewState["sortdirection"].ToString() == "asc")
            {
                ViewState["sortdirection"] = "desc";
            }
            else
            {
                ViewState["sortdirection"] = "asc";
            }
        }
        BindGrid();
    }
很明顯,設置viewsate來保存每次排序時的順序,上面的相信很容易理解。
     最后,實現(xiàn)編輯功能,因為在aspx頁面中已經(jīng)設置了OnRowEditing="GridView1_RowEditing",其中GridView1_RowEditing的代碼為
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int empid;
        string fname, lname;
        empid = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
        fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        lname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

        SqlConnection cnn = new SqlConnection(@"data source=localhost;initial catalog=northwind;user id=sa;passWord=123456");
        cnn.Open();
        SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid", cnn);
        cmd.Parameters.Add(new SqlParameter("@fname",fname));
        cmd.Parameters.Add(new SqlParameter("@lname", lname));
        cmd.Parameters.Add(new SqlParameter("@empid", empid));
        cmd.ExecuteNonQuery();
        cnn.Close();

        GridView1.EditIndex = -1;
        BindGrid();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }

  可以看到,上面的代碼和asp.net 1.1版本的其實原理是差不多的。最后,bindgrid()的過程很簡單,為綁定咯
DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
        da.Fill(ds,"employees");
        DataView dv = ds.Tables[0].DefaultView;

        if (ViewState["sortexpression"] != null)
        {
            dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
        }

        GridView1.DataSource=dv;
        GridView1.DataBind();

這里gridview綁定的是dataview,并且用dv.sort設定了每次排序的順序,也就是說,每次排序后其順序都是保持不變的。
當然最后是page_load事件咯
   protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGrid();
        }
    }

http://jackyrong.VEVb.com/archive/2006/07/20/455996.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长乐市| 安泽县| 崇仁县| 陆良县| 大安市| 冀州市| 东阿县| 奇台县| 汶川县| 九台市| 嘉荫县| 福鼎市| 宝丰县| 五指山市| 错那县| 萨迦县| 修文县| 揭西县| 克东县| 外汇| 安康市| 揭东县| 密云县| 潍坊市| 吐鲁番市| 共和县| 平昌县| 建湖县| 邢台市| 南昌县| 凌云县| 西城区| 措勤县| 封开县| 睢宁县| 特克斯县| 惠东县| 彰化市| 布尔津县| 嘉义县| 博湖县|