asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)

翻译|其它|编辑:郝浩|2007-10-26 13:13:59.000|阅读 854 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

      asp.net 2.0下一个标准 GRIDVIEW 功能的实现(不用 datasource 控件)使用 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" />
            <agerStyle 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();
    }

      为了实现每列都可以自动点击排序,可以设置 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 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();
        }
    }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP