




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1.GridView无代码分页排序:效果图:1.AllowSorting设为True,aspx代码中是AllowSorting=True;2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize=12。3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。2.GridView选中,编辑,取消,删除:效果图:后台代码:你可以使用sqlhelper,本文没用。代码如下:using System;using System.Data;using System.Configuration;using System
2、.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.SqlClient;public partial class _Default : System.Web.UI.Page /清清月儿/21aspnet SqlConnection sqlcon; SqlComma
3、nd sqlcom; string strCon = Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码; protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) bind(); protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) GridView1.EditIndex = e.NewEditIndex; bind(); /删除 protected void GridV
4、iew1_RowDeleting(object sender, GridViewDeleteEventArgs e) string sqlstr = delete from 表 where id= + GridView1.DataKeyse.RowIndex.Value.ToString() + ; sqlcon = new SqlConnection(strCon); sqlcom = new SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); /更新 prot
5、ected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) sqlcon = new SqlConnection(strCon); string sqlstr = update 表 set 字段1= + (TextBox)(GridView1.Rowse.RowIndex.Cells1.Controls0).Text.ToString().Trim() + ,字段2= + (TextBox)(GridView1.Rowse.RowIndex.Cells2.Controls0).Text.ToString(
6、).Trim() + ,字段3= + (TextBox)(GridView1.Rowse.RowIndex.Cells3.Controls0).Text.ToString().Trim() + where id= + GridView1.DataKeyse.RowIndex.Value.ToString() + ; sqlcom=new SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; bind(); /取消 protecte
7、d void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) GridView1.EditIndex = -1; bind(); /绑定 public void bind() string sqlstr = select * from 表; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon
8、.Open(); myda.Fill(myds, 表); GridView1.DataSource = myds; GridView1.DataKeyNames = new string id ;/主键 GridView1.DataBind(); sqlcon.Close(); 前台主要代码: . . 3.GridView正反双向排序:效果图:点姓名各2次的排序,点其他也一样可以。后台代码:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;usi
9、ng 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.SqlClient;public partial class Default3 : System.Web.UI.Page/清清月儿的博客/21aspnet SqlConnection sqlcon; string strCon
10、= Data Source=(local);Database=北风贸易;Uid=sa;Pwd=; protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) ViewStateSortOrder = 身份证号码; ViewStateOrderDire = ASC; bind(); protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) string sPage = e.SortExpression; if (ViewState
11、SortOrder.ToString() = sPage) if (ViewStateOrderDire.ToString() = Desc) ViewStateOrderDire = ASC; else ViewStateOrderDire = Desc; else ViewStateSortOrder = e.SortExpression; bind(); public void bind() string sqlstr = select top 5 * from 飞狐工作室; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda
12、= new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, 飞狐工作室); DataView view = myds.Tables飞狐工作室.DefaultView; string sort = (string)ViewStateSortOrder + + (string)ViewStateOrderDire; view.Sort = sort; GridView1.DataSource = view; GridView1.DataBind(); sqlco
13、n.Close(); 前台主要代码: 4.GridView和下拉菜单DropDownList结合:效果图:后台代码: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.Htm
14、lControls;using System.Data.SqlClient;public partial class Default4 : System.Web.UI.Page SqlConnection sqlcon; string strCon = Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa; protected void Page_Load(object sender, EventArgs e) DropDownList ddl; if (!IsPostBack) string sqlstr = select top 5 * from
15、飞狐工作室; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, 飞狐工作室); GridView1.DataSource = myds; GridView1.DataBind(); for (int i = 0; i = GridView1.Rows.Count - 1; i+) DataRowView mydrv = myds.Tabl
16、es飞狐工作室.DefaultViewi; if (Convert.ToString(mydrv员工性别).Trim() = True) ddl = (DropDownList)GridView1.Rowsi.FindControl(DropDownList1); ddl.SelectedIndex = 0; if (Convert.ToString(mydrv员工性别).Trim() = False) ddl = (DropDownList)GridView1.Rowsi.FindControl(DropDownList1); ddl.SelectedIndex = 1; sqlcon.Cl
17、ose(); public SqlDataReader ddlbind() string sqlstr = select distinct 员工性别 from 飞狐工作室; sqlcon = new SqlConnection(strCon); SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); return sqlcom.ExecuteReader(); 前台主要代码: asp:DropDownList ID=DropDownList1 runat=server DataSource= DataValueFie
18、ld=员工性别 DataTextField=员工性别 5.GridView和CheckBox结合:效果图:后台代码: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 System.Web.UI.HtmlControls;using System.Da
19、ta.SqlClient;public partial class Default5 : System.Web.UI.Page/清清月儿/21aspnet SqlConnection sqlcon; string strCon = Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa; protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) bind(); protected void CheckBox2_CheckedChange
20、d(object sender, EventArgs e) for (int i = 0; i = GridView1.Rows.Count - 1; i+) CheckBox cbox = (CheckBox)GridView1.Rowsi.FindControl(CheckBox1); if (CheckBox2.Checked = true) cbox.Checked = true; else cbox.Checked = false; protected void Button2_Click(object sender, EventArgs e) sqlcon = new SqlCon
21、nection(strCon); SqlCommand sqlcom; for (int i = 0; i = GridView1.Rows.Count - 1; i+) CheckBox cbox = (CheckBox)GridView1.Rowsi.FindControl(CheckBox1); if (cbox.Checked = true) string sqlstr = delete from 飞狐工作室 where 身份证号码= + GridView1.DataKeysi.Value + ; sqlcom = new SqlCommand(sqlstr, sqlcon); sql
22、con.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); protected void Button1_Click(object sender, EventArgs e) CheckBox2.Checked = false; for (int i = 0; i = GridView1.Rows.Count - 1; i+) CheckBox cbox = (CheckBox)GridView1.Rowsi.FindControl(CheckBox1); cbox.Checked = false; public void bind
23、() 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, tb_Member); GridView1.DataSource = myds; GridView1.DataKeyNames = new string 身份证号码 ; GridView1.Data
24、Bind(); sqlcon.Close(); 前台主要代码: 6.鼠标移到GridView某一行时改变该行的背景色方法一:效果图:做法:双击GridView的OnRowDataBound事件;在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) int i; /执行循环,保证每条数据都可以更新 for (i = 0; i GridView1.Rows.Count; i+) /首先判断是否是数据行 if (e
25、.Row.RowType = DataControlRowType.DataRow) /当鼠标停留时更改背景色 e.Row.Attributes.Add(onmouseover, c=this.style.backgroundColor;this.style.backgroundColor=#00A9FF); /当鼠标移开时还原背景色 e.Row.Attributes.Add(onmouseout, this.style.backgroundColor=c); 前台代码: 实现鼠标划过改变GridView的行背景色 清清月儿/21aspnet asp:Sq
26、lDataSource ID=SqlDataSource1 runat=server ConnectionString= SelectCommand=SELECT top 5 身份证号码, 姓名, 员工性别, 家庭住址, 邮政编码 FROM 飞狐工作室 DataSourceMode=DataReader 7.鼠标移到GridView某一行时改变该行的背景色方法二:效果图:做法:和上面的一样就是代码不同protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) /int i; /执行循环,保证每条数据都
27、可以更新 /for (i = 0; i GridView1.Rows.Count; i+) / / /首先判断是否是数据行 / if (e.Row.RowType = DataControlRowType.DataRow) / / /当鼠标停留时更改背景色 / e.Row.Attributes.Add(onmouseover, c=this.style.backgroundColor;this.style.backgroundColor=#00A9FF); / /当鼠标移开时还原背景色 / e.Row.Attributes.Add(onmouseout, this.style.backgroundColor=c); / / /如果是绑定数据行 if (e.Row.RowType = DataControlRowType.DataRow) /鼠标经过时,行背景色变 e.Row.Attributes.Add(onmouseover, this.style.backgroundColor=#E6F5FA); /鼠标移出时,行背景色变 e.Row.Attributes.Ad
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025建筑工程技术人员招聘笔试题库及答案
- 人工智能2025年二季度投融市场报告
- 2025年靖西小学面试题及答案
- 2025年量化公募面试题及答案
- 2025年疾概试题及答案
- 2025年河南竞赛题库
- 2025年六抗疫知识竞赛题库
- 2025年父亲的病测试题及答案
- 2025年吞云吐雾测试题及答案解析
- 2025年红楼梦简答试题及答案
- 习近平总书记关于教育的重要论述研究(安庆师范大学版)学习通超星课后章节答案期末考试题库2023年
- 餐厅杂物电梯事故应急预案
- 地表水体长度和面积遥感监测技术规范
- 工程项目档案试题
- 银行账户基本信息表
- THBFIA 0004-2020 红枣制品标准
- GB/T 2652-1989焊缝及熔敷金属拉伸试验方法
- GB/T 24824-2009普通照明用LED模块测试方法
- GA/T 543.5-2012公安数据元(5)
- 公益事业捐赠预评估表
- 养老护理员实操考核标准
评论
0/150
提交评论