经典gridview72种使用技巧

上传人:腾**** 文档编号:40353505 上传时间:2018-05-26 格式:DOC 页数:57 大小:483.50KB
返回 下载 相关 举报
经典gridview72种使用技巧_第1页
第1页 / 共57页
经典gridview72种使用技巧_第2页
第2页 / 共57页
经典gridview72种使用技巧_第3页
第3页 / 共57页
经典gridview72种使用技巧_第4页
第4页 / 共57页
经典gridview72种使用技巧_第5页
第5页 / 共57页
点击查看更多>>
资源描述

《经典gridview72种使用技巧》由会员分享,可在线阅读,更多相关《经典gridview72种使用技巧(57页珍藏版)》请在金锄头文库上搜索。

1、首先感谢原作者无私的奉献!首先感谢原作者无私的奉献!GridView 72 般绝技般绝技快速预览:GridView 无代无代码码分分页排序页排序GridView 选中,编辑,取消,删除选中,编辑,取消,删除GridView 正反双向排序正反双向排序GridView 和下拉菜单和下拉菜单 DropDownList 结合结合GridView 和和 CheckBox 结合结合鼠标移到鼠标移到 GridView 某一行时改变该行的背景色方法一某一行时改变该行的背景色方法一鼠标移到鼠标移到 GridView 某一行时改变该行的背景色方法二某一行时改变该行的背景色方法二GridView 实现删除时弹出确认

2、对话框实现删除时弹出确认对话框GridView 实现自动编号实现自动编号GridView 实现自定义时间货币等字符串格式实现自定义时间货币等字符串格式GridView 实现用实现用“.”代替超长字符串代替超长字符串GridView 一般换行与强制换行一般换行与强制换行GridView 显示隐藏某一列显示隐藏某一列GridView 弹出新页面弹出新页面/弹出新窗口弹出新窗口GridView 固定表头固定表头(不用不用 javascript 只用只用 CSS,2 行代码,很好用行代码,很好用)GridView 合并表头多重表头无错完美版(以合并合并表头多重表头无错完美版(以合并 3 列列 3 行举

3、例)行举例)GridView 突出显示某一单元格(例如金额低于多少,分数不及格等)突出显示某一单元格(例如金额低于多少,分数不及格等)GridView 加入自动求和求平均值小计加入自动求和求平均值小计GridView 数据导入数据导入 Excel/Excel 数据读入数据读入 GridView1.GridView 无代码分页排序无代码分页排序:效果图:1.AllowSorting 设为 True,aspx 代码中是 AllowSorting=“True“;2.默认 1 页 10 条,如果要修改每页条数,修改 PageSize 即可,在 aspx 代码中是 PageSize=“12“。3.默认的

4、是单向排序的,右击 GridView 弹出“属性”,选择 AllowSorting 为 True 即可。2.GridView 选中,编辑,取消,删除:选中,编辑,取消,删除:效果图:后台代码:后台代码:using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using

5、 System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page SqlConnection sqlcon;SqlCommand sqlcom;string strCon = “Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码“;protected void Page_Load(object sender, EventArgs e)if (!IsPostBack)bind();protected voi

6、d GridView1_RowEditing(object sender, GridViewEditEventArgs e)GridView1.EditIndex = e.NewEditIndex;bind();/删除protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)string sqlstr = “delete from 表 where id=“ + GridView1.DataKeyse.RowIndex.Value.ToString() + “;sqlcon = new SqlCon

7、nection(strCon);sqlcom = new SqlCommand(sqlstr,sqlcon);sqlcon.Open();sqlcom.ExecuteNonQuery();sqlcon.Close();bind();/更新protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)sqlcon = new SqlConnection(strCon);string sqlstr = “update 表 set 字段 1=“+ (TextBox)(GridView1.Rowse.RowI

8、ndex.Cells1.Controls0).Text.ToString().Trim() + “,字段 2=“+ (TextBox)(GridView1.Rowse.RowIndex.Cells2.Controls0).Text.ToString().Trim() + “,字段 3=“+ (TextBox)(GridView1.Rowse.RowIndex.Cells3.Controls0).Text.ToString().Trim() + “ where id=“ + GridView1.DataKeyse.RowIndex.Value.ToString() + “;sqlcom=new

9、SqlCommand(sqlstr,sqlcon);sqlcon.Open();sqlcom.ExecuteNonQuery();sqlcon.Close();GridView1.EditIndex = -1;bind();/取消protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)GridView1.EditIndex = -1;bind();/绑定public void bind()string sqlstr = “select * from 表“;sqlcon = ne

10、w SqlConnection(strCon);SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);DataSet myds = new DataSet();sqlcon.Open();myda.Fill(myds, “表“);GridView1.DataSource = myds;GridView1.DataKeyNames = new string “id“ ;/主键GridView1.DataBind();sqlcon.Close();前台主要代码:前台主要代码:. .3.GridView 正反双向排序正反双向排序:效果图:点

11、姓名各 2 次的排序,点其他也一样可以。后台代码:后台代码:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.

12、SqlClient;public partial class Default3 : System.Web.UI.PageSqlConnection sqlcon;string strCon = “Data Source=(local);Database=北风贸易;Uid=sa;Pwd=“;protected void Page_Load(object sender, EventArgs e)if (!IsPostBack)ViewState“SortOrder“ = “身份证号码“;ViewState“OrderDire“ = “ASC“;bind();protected void GridV

13、iew1_Sorting(object sender, GridViewSortEventArgs e)string sPage = e.SortExpression;if (ViewState“SortOrder“.ToString() = sPage)if (ViewState“OrderDire“.ToString() = “Desc“)ViewState“OrderDire“ = “ASC“;elseViewState“OrderDire“ = “Desc“;elseViewState“SortOrder“ = e.SortExpression;bind();public void b

14、ind()string sqlstr = “select top 5 * from 飞狐工作室“;sqlcon = new SqlConnection(strCon);SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);DataSet myds = new DataSet();sqlcon.Open();myda.Fill(myds, “飞狐工作室“);DataView view = myds.Tables“飞狐工作室“.DefaultView;string sort = (string)ViewState“SortOrder“ +

15、 “ “ + (string)ViewState“OrderDire“;view.Sort = sort;GridView1.DataSource = view;GridView1.DataBind();sqlcon.Close();前台主要代码:前台主要代码:4.GridView 和下拉菜单和下拉菜单 DropDownList 结合结合:效果图:效果图:后台代码:后台代码:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using Syste

16、m.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class Default4 : System.Web.UI.PageSqlConnection sqlcon;string strCon = “Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa“;protected void Page_Load(object sender, EventArgs e)DropDownList ddl;if (!IsPostB

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号