程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> asp.net 2.0下一個標准GRIDVIEW功能的實現(不用datasource控件)

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

編輯:.NET實例教程


在ASP.Net 2.0下,gridview是十分方便的了,加一個DATASOURCE系列的控件的話,就可以馬上和gridvIEw綁定,十分方便。但其實也可以
使用datatable或者datavIEw的,這個時候就不是用datasource系列控件了。下面講下如何在ASP.Net 2.0下,實現gridvIEw控件的翻頁,各列排序,
編輯的功能。
    首先,我們讀取的是northwind數據庫中的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" SortExpression="firstname" />
                <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>

首先,我們要實現分頁,把AllowPaging設置為true,並設置每頁的分頁條數,最後在codebehind中寫入
protected void GridView1_PageIndexChanging(object sender, GridVIEwPageEventArgs e)
    {
        GridVIEw1.PageIndex = e.NewPageIndex;
        BindGrid();
&nb
sp;   }
  為了實現每列都可以自動點擊排序,可以設置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來保存每次排序時的順序,上面的相信很容易理解。
     最後,實現編輯功能,因為在ASPx頁面中已經設置了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 SqlPa
rameter("@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.cnblogs.com/archive/2006/07/20/455996.Html


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved