GridView的初级使用
使用GridView自带的分页功能,需要激发PageIndexChanging
protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//获取当前页的索引。
gvNewsList.PageIndex = e.NewPageIndex;
bind();
}
GridView自带的排序功能
在页面代码中需要设置AllowSorting="True"
同时还需要在您要排序的列表头字段后写出排序字段如:
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"/>
首先需要确定第一次加载的时候根据哪个字段进行升序还是降序
于是在首次加载时候需要存两个参数
代码如下:
if (!IsPostBack)
{
ViewState["SortOrder"] = "ID";
ViewState["OrderDire"] = "ASC";
this.bind();
}
在数据绑定方法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();
}
需要激发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();
}
GridView自带的编辑事件。
1 编辑列的时候添加CommandField字段。编辑则设置其ShowEditButton属性为true,ShowEditButton为true。
2 使用GridView自带的编辑事件需要激活RowEditing事件
代码如下:
protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
{
//设置GrideView控件的编辑项的索引为选择的当前索引
gvNewsList.EditIndex = e.NewEditIndex;
//数据绑定
bind();
}
3 单击取消更新按钮的话,需要激活RowCancelingEdit
代码如下:
protected void gvNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//将当前编辑项的索引设为-1,则为取消编辑。
gvNewsList.EditIndex = -;
bind();
}
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[].Controls[])).Text.Trim().ToString();
//获取类别
string style = ((TextBox)(gvNewsList.Rows[e.RowIndex].Cells[].Controls[])).Text.Trim().ToString();
//获取日期
string adddate = ((TextBox)(gvNewsList.Rows[e.RowIndex].Cells[].Controls[])).Text.Trim().ToString();
string sql = "update tb_News set title ='" + title + "', [Style]='" + style + "',issueDate=" + adddate + " where id = " + id;
CC.ExecSQL(sql);
//更新完成后退出编辑状态,重新绑定一次数据
gvNewsList.EditIndex = -;
bind();
}
高亮显示光标所在行
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");
}
}
设置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[].Text = Convert.ToDateTime(e.Row.Cells[].Text).ToString("yyyy-mm-dd");
}
}
货币的转换格式如下所示:string result = String.Format("{0,C2}",Convert.ToDouble(result));
单击GridView控件某行的按钮,刷新页面后不会回到页面顶端
网页刷新后维持原位置
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true"%>
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();
}
在单击“删除”按钮的时候弹出确认框。
protected void gvNewsList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)(e.Row.Cells[].Controls[])).Attributes.Add("onclick","return confirm('确定要删除吗?')");
}
}
GridView的初级使用的更多相关文章
- GridView控件详解
一.介绍 GridView控件一表格形式显示数据源中的数据.提供对列进行排序.分页以及编辑.删除单个记录的功能. 二.绑定数据源 第一种使用DataSourceID属性.可以直接把GridView控件 ...
- Android GridView 通过seletor 设置状态和默认状态
Android中可以通过selector控制GridView Item 的状态,而省去使用代码控制 GridView View Selector Xml文件 <?xml version=&quo ...
- Asp.Net 操作XML文件的增删改查 利用GridView
不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...
- 在DevExpress程序中使用GridView直接录入数据的时候,增加列表选择的功能
在我上篇随笔<在DevExpress程序中使用Winform分页控件直接录入数据并保存>中介绍了在GridView以及在其封装的分页控件上做数据的直接录入的处理,介绍情况下数据的保存和校验 ...
- Android listview和gridview以及view的区别
GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...
- 马哥linux运维初级+中级+高级 视频教程 教学视频 全套下载(近50G)
马哥linux运维初级+中级+高级 视频教程 教学视频 全套下载(近50G)目录详情:18_02_ssl协议.openssl及创建私有CA18_03_OpenSSH服务及其相关应用09_01_磁盘及文 ...
- 在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView
背景 在前一篇文章<[初学者指南]在ASP.NET MVC 5中创建GridView>中,我们学习了如何在 ASP.NET MVC 中实现 GridView,类似于 ASP.NET web ...
- 【初学者指南】在ASP.NET MVC 5中创建GridView
介绍 在这篇文章中,我们将会学习如何在 ASP.NET MVC 中创建一个 gridview,就像 ASP.NET Web 表单中的 gridview 一样.服务器端和客户端有许多可用的第三方库,这些 ...
- ScrollView嵌套ListView,GridView数据加载不全问题的解决
我们大家都知道ListView,GridView加载数据项,如果数据项过多时,就会显示滚动条.ScrollView组件里面只能包含一个组件,当ScrollView里面嵌套listView,GridVi ...
随机推荐
- 终端上设置git
http://blog.163.com/xianfuying@126/blog/static/21960005201181482518631/ 在-/.ssh的位置vi id_rsa.pub 拷贝的时 ...
- animate基础
用JQUERY做动画是很方便的,已经看过大牛们做出不逊色于FLASH的各种效果. 其中的基本功就有animate这个方法的使用.于是,从零开始,训练基本功: <body> <div ...
- Delphi中的THashTable
在Delphi中,Inifiles单元中有一个TStringHash的类,不过它的Value仅支持Integer(其实也不是问题,有其它类型可以将变量变为Pointer),有点不舒服,今天没事做就把它 ...
- Android Studio使用远程依赖时下载不了jar包的解决方法
使用AS很大的一个好处就是可以使用在线jar包,只需在引用jar包的时候在版本后加上+,比如: compile 'com.facebook.fresco:fresco:0.1.0+' 这样不用在jar ...
- YII 主题
heming是一个在Web应用程序里定制网页外观的系统方式.通过采用一个新的主题,网页应用程序的整体外观可以立即和戏剧性的改变. 在Yii,每个主题由一个目录代表,包含view文件,layout文件和 ...
- Hibernate 配置详解(3)
7) hibernate.max_fetch_depth: 该属性用于设置left out join单对象查询关系(one-to-one/many-to-one)中最大的关联深度.默认值为0,即默认情 ...
- (转载)php数组添加、删除元素的方法
(转载)http://www.phpgs.com/html/php/phpjichu/20120130440.html 带来一篇php 数组 添加元素.删除元素的方法的文章,有需要的php学习者参考下 ...
- 杂题 UVAoj 10000 Longest Paths
Longest Paths It is a well known fact that some people do not have their social abilities complet ...
- Linux下动态库的使用
1.生成动态库: gcc -fPIC -shared -o libdemo.so demo.c 考虑程式库major的升级会破坏兼容性:而minior的升级则可能不会,一般建议用以下方式来生成动态库. ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...