GridView的初级使用

使用GridView自带的分页功能,需要激发PageIndexChanging

protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //获取当前页的索引。
        gvNewsList.PageIndex = e.NewPageIndex;
        bind();
    }
View Code

GridView自带的排序功能
在页面代码中需要设置AllowSorting="True"
同时还需要在您要排序的列表头字段后写出排序字段如:
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"/>
首先需要确定第一次加载的时候根据哪个字段进行升序还是降序
于是在首次加载时候需要存两个参数
代码如下:

if (!IsPostBack)
        {
            ViewState["SortOrder"] = "ID";
            ViewState["OrderDire"] = "ASC";
            this.bind();
        }
View Code

在数据绑定方法bind()中,我们使用DateView来进行排序和存储数据。

protected void bind()
    {
        //调用CommonClass类的GetDataSet方法,查询需要管理的新闻信息,并绑定GridView控件上
        //this.gvNewsList.DataSource = CC.GetDataSet("select * from tb_News order by id", "tbNews");
        //this.gvNewsList.DataBind();
        //实例化SqlDataAdapter对象
        SqlDataAdapter da = new SqlDataAdapter("select * from tb_News", CC.GetConnection());
        //实例化数据集
        DataSet ds = new DataSet();
        da.Fill(ds,"tb_News");
        DataView dv = ds.Tables["tb_News"].DefaultView;
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        dv.Sort = sort;
    //绑定控件
        this.gvNewsList.DataSource = dv;
        this.gvNewsList.DataBind();
    }
View Code

需要激发Sorting事件

protected void gvNewsList_Sorting(object sender, GridViewSortEventArgs e)
    {
        //获取指定的表达式,即字段。根据哪个字段来排列的。
        string sPage = e.SortExpression;
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
            {
                ViewState["OrderDire"] = "ASC";
            }
            else
            {
                ViewState["OrderDire"] = "Desc";
            }
        }
        else
        {
            ViewState["SortOrder"] = sPage;
        }
        bind();
    }
View Code

GridView自带的编辑事件。
1 编辑列的时候添加CommandField字段。编辑则设置其ShowEditButton属性为true,ShowEditButton为true。
2 使用GridView自带的编辑事件需要激活RowEditing事件
代码如下:

protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //设置GrideView控件的编辑项的索引为选择的当前索引
        gvNewsList.EditIndex = e.NewEditIndex;
        //数据绑定
        bind();
    }
View Code

3 单击取消更新按钮的话,需要激活RowCancelingEdit
代码如下:

protected void gvNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //将当前编辑项的索引设为-1,则为取消编辑。
        gvNewsList.EditIndex = -1;
        bind();
    }
View Code

4 修改完毕数据,单击更新按钮需要激活RowUpdating事件
需要在页面中设置DataKeyNames: DataKeyNames="ID"
RowUpdating事件中的代码如下:

protected void gvNewsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取id值
        string id = gvNewsList.DataKeys[e.RowIndex].Value.ToString();
        //获取文本框中输入的内容
        string title = ((TextBox)(gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        //获取类别
        string style = ((TextBox)(gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        //获取日期
        string adddate = ((TextBox)(gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        string sql = "update tb_News set title ='" + title + "', [Style]='" + style + "',issueDate=" + adddate + " where id = " + id;
        CC.ExecSQL(sql);
        //更新完成后退出编辑状态,重新绑定一次数据
        gvNewsList.EditIndex = -1;
        bind();
    }
View Code

高亮显示光标所在行

protected void gvNewsList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.syle.backgroundColor='#6699ff'");
            e.Row.Attributes.Add("onmouseout", "this.syle.backgroundColor=currentcolor");
        }
    }
View Code

设置GridView控件的数据显示格式
实现思路:主要在RowDataBound事件中实现。当数据源绑定到GridView控件中的每行时,将触发该控件的RowDataBound。修改或设置绑定到该行的数据的显示格式,可以使用RowDataBound事件的GridViewEventArgs e参数的Row属性的Cells属性定位到指定单元格。

protected void gvNewsList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Text = Convert.ToDateTime(e.Row.Cells[3].Text).ToString("yyyy-mm-dd");
        }
    }
View Code

货币的转换格式如下所示:string result = String.Format("{0,C2}",Convert.ToDouble(result));

单击GridView控件某行的按钮,刷新页面后不会回到页面顶端
网页刷新后维持原位置

<%@ Page Language="C#" MaintainScrollPositionOnPostback="true"%>
View Code

GridView自带删除事件,添加一个CommandField列并指名为“删除”按钮,单击该按钮时触发RowDeleting事件

 protected void gvNewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gvNewsList.DataKeys[e.RowIndex].Value.ToString();
        string sql = "delete from tb_News where id = "+id;
        CC.ExecSQL(sql);
        bind();
    }
View Code

在单击“删除”按钮的时候弹出确认框。

protected void gvNewsList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick","return confirm('确定要删除吗?')");
        }
    }
View Code
原文地址:https://www.cnblogs.com/Jokers/p/3544903.html