Gridview.docx
- 文档编号:12369228
- 上传时间:2023-04-18
- 格式:DOCX
- 页数:58
- 大小:29.66KB
Gridview.docx
《Gridview.docx》由会员分享,可在线阅读,更多相关《Gridview.docx(58页珍藏版)》请在冰豆网上搜索。
Gridview
可以在Gridview的RowDataBound事件中实现代码
1.
2.protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
3.{
4.if(e.Row.RowType==DataControlRowType.DataRow)
5.{
6.foreach(TableCellcelline.Row.Cells)
7.{
8.//单击鼠标可以将单元格中的内容复制到剪切板
9.cell.Attributes.Add("onclick","window.clipboardData.setData('Text',this.innerText);");
10.//鼠标移入可以设置单元格字体颜色
11.cell.Attributes.Add("onmouseover","this.style.color='red'");
12.//鼠标移开,取消单元格字体颜色设置
13.cell.Attributes.Add("onmouseout","this.style.color=''");
14.
15.}
16.}
17.}
★
GridView控件实现支持分页的自动编号代码
错误只有本页编号的写法
TemplateFieldHeaderText="序号"> <%#Container.DataItemIndex+1%>
TemplateField>
正确的如下(参考其它人的忘了地址保存文本)
前台
TemplateFieldHeaderText="序号"> <%#(this.Pager.CurrentPageIndex-1)*this.Pager.PageSize+Container.DataItemIndex+1%>
TemplateField>
后台
在RowDataBound事件中加
if(e.Row.RowIndex>-1)
{
e.Row.Cells[0].Text=Convert.ToString((this.Pager.CurrentPageIndex-1)*this.Pager.PageSize+e.Row.RowIndex+1);
}
这就OK了,前台,后台只用一个即可,Pager是用的分页控件,这种分页控件都是从1开始,所以要进行减一的操作,如果用PagedDataSourceps=newPagedDataSource();ps.CurrentPageIndex 就不用进行减一,因为他是从0开始相加的。
★
GridView加入自动求和求平均值小计
GridView加入自动求和求平均值小计
效果图:
解决方案:
1privatedoublesum=0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
2protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
3{
4if(e.Row.RowIndex>=0)
5{
6sum+=Convert.ToDouble(e.Row.Cells[6].Text);
7}
8elseif(e.Row.RowType==DataControlRowType.Footer)
9{
10e.Row.Cells[5].Text="总薪水为:
";
11e.Row.Cells[6].Text=sum.ToString();
12e.Row.Cells[3].Text="平均薪水为:
";
13e.Row.Cells[4].Text=((int)(sum/GridView1.Rows.Count)).ToString();
14}
15}
后台全部代码:
1usingSystem;
2usingSystem.Data;
3usingSystem.Configuration;
4usingSystem.Web;
5usingSystem.Web.Security;
6usingSystem.Web.UI;
7usingSystem.Web.UI.WebControls;
8usingSystem.Web.UI.WebControls.WebParts;
9usingSystem.Web.UI.HtmlControls;
10usingSystem.Data.SqlClient;
11usingSystem.Drawing;
12publicpartialclassDefault7:
System.Web.UI.Page
13{
14SqlConnectionsqlcon;
15SqlCommandsqlcom;
16stringstrCon="DataSource=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17protectedvoidPage_Load(objectsender,EventArgse)
18{
19if(!
IsPostBack)
20{
21bind();
22}
23}
24protectedvoidGridView1_RowEditing(objectsender,GridViewEditEventArgse)
25{
26GridView1.EditIndex=e.NewEditIndex;
27bind();
28}
29protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse)
30{
31sqlcon=newSqlConnection(strCon);
32stringsqlstr="update飞狐工作室set姓名='"
33+((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()+"',家庭住址='"
34+((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()+"'where身份证号码='"
35+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
36sqlcom=newSqlCommand(sqlstr,sqlcon);
37sqlcon.Open();
38sqlcom.ExecuteNonQuery();
39sqlcon.Close();
40GridView1.EditIndex=-1;
41bind();
42}
43protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse)
44{
45GridView1.EditIndex=-1;
46bind();
47}
48publicvoidbind()
49{
50stringsqlstr="selecttop5*from飞狐工作室";
51sqlcon=newSqlConnection(strCon);
52SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon);
53DataSetmyds=newDataSet();
54sqlcon.Open();
55myda.Fill(myds,"飞狐工作室");
56GridView1.DataSource=myds;
57GridView1.DataKeyNames=newstring[]{"身份证号码"};
58GridView1.DataBind();
59sqlcon.Close();
60}
61privatedoublesum=0;//取指定列的数据和
62protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
63{
64if(e.Row.RowIndex>=0)
65{
66sum+=Convert.ToDouble(e.Row.Cells[6].Text);
67}
68elseif(e.Row.RowType==DataControlRowType.Footer)
69{
70e.Row.Cells[5].Text="总薪水为:
";
71e.Row.Cells[6].Text=sum.ToString();
72e.Row.Cells[3].Text="平均薪水为:
";
73e.Row.Cells[4].Text=((int)(sum/GridView1.Rows.Count)).ToString();
74}
75}
76}
前台:
唯一的花头就是设置ShowFooter="True",否则默认表头为隐藏的!
1 GridViewID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="3"OnRowEditing="GridView1_RowEditing" 2OnRowUpdating="GridView1_RowUpdating"OnRowCancelingEdit="GridView1_RowCancelingEdit"BackColor="White"BorderColor="#CCCCCC"BorderStyle="None"BorderWidth="1px"Font-Size="12px"OnRowDataBound="GridView1_RowDataBound"ShowFooter="True"> 3 4 5 CommandFieldHeaderText="编辑"ShowEditButton="True"/> 6 BoundFieldDataField="身份证号码"HeaderText="编号"ReadOnly="True"/> 7 BoundFieldDataField="姓名"HeaderText="姓名"/> 8 BoundFieldDataField="出生日期"HeaderText="邮政编码"/> 9 BoundFieldDataField="家庭住址"HeaderText="家庭住址"/> 10 BoundFieldDataField="邮政编码"HeaderText="邮政编码"/> 11 BoundFieldDataField="起薪"HeaderText="起薪"/> 12 13 14 15 16 17 18 GridView> ★ GridView合并表头多重表头(以合并3列3行举例) GridView合并表头多重表头无错完美版(以合并3列3行举例) 效果图: 后台代码: 1usingSystem; 2usingSystem.Data; 3usingSystem.Configuration; 4usingSystem.Web; 5usingSystem.Web.Security; 6usingSystem.Web.UI; 7usingSystem.Web.UI.WebControls; 8usingSystem.Web.UI.WebControls.WebParts; 9usingSystem.Web.UI.HtmlControls; 10usingSystem.Data.SqlClient; 11usingSystem.Drawing; 12publicpartialclass_Default: System.Web.UI.Page 13{ 14SqlConnectionsqlcon; 15SqlCommandsqlcom; 16stringstrCon="DataSource=(local);Database=北风贸易;Uid=sa;Pwd=sa"; 17protectedvoidPage_Load(objectsender,EventArgse) 18{ 19if(! IsPostBack) 20{ 21bind(); 22 23} 24} 25protectedvoidGridView1_RowEditing(objectsender,GridViewEditEventArgse) 26{ 27GridView1.EditIndex=e.NewEditIndex; 28bind(); 29} 30protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse) 31{ 32sqlcon=newSqlConnection(strCon); 33stringsqlstr="update飞狐工作室set姓名='" 34+((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()+"',家庭住址='" 35+((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()+"'where身份证号码='" 36+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'"; 37sqlcom=newSqlCommand(sqlstr,sqlcon); 38sqlcon.Open(); 39sqlcom.ExecuteNonQuery(); 40sqlcon.Close(); 41GridView1.EditIndex=-1; 42bind(); 43} 44protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse) 45{ 46GridView1.EditIndex=-1; 47bind(); 48} 49publicvoidbind() 50{ 51stringsqlstr="selecttop10*from飞狐工作室"; 52sqlcon=newSqlConnection(strCon); 53SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon); 54DataSetmyds=newDataSet(); 55sqlcon.Open(); 56myda.Fill(myds,"飞狐工作室"); 57GridView1.DataSource=myds; 58GridView1.DataKeyNames=newstring[]{"身份证号码"}; 59GridView1.DataBind(); 60sqlcon.Close(); 61} 62 63//这里就是解决方案 64protectedvoidGridView1_RowCreated(objectsender,GridViewRowEventArgse) 65{ 66switch(e.Row.RowType) 67{ 68caseDataControlRowType.Header: 69//第一行表头 70TableCellCollectiontcHeader=e.Row.Cells; 71tcHeader.Clear(); 72tcHeader.Add(newTableHeaderCell()); 73tcHeader[0].Attributes.Add("rowspan","3");//跨Row 74tcHeader[0].Attributes.Add("bgcolor","white"); 75tcHeader[0].Text=""; 76tcHeader.Add(newTableHeaderCell()); 77//tcHeader[1].Attributes.Add("bgcolor","Red"); 78tcHeader[1].Attributes.Add("colspan","6");//跨Column 79tcHeader[1].Text="全部信息 80 81//第二行表头 82tcHeader.Add(newTableHeaderCell()); 83tcHeader[2].Attributes.Add("bgcolor","DarkSeaGreen"); 84tcHeader[2].Text="身份证号码"; 85tcHeader.Add(newTableHeaderCell()); 86tcHeader[3].Attributes.Add("bgcolor","LightSteelBlue"); 87tcHeader[3].Attributes.Add("colspan","2"); 88tcHeader[3].Text="基本信息"; 89tcHeader.Add(newTableHeaderCell()); 90tcHeader[4].Attributes.Add("bgcolor","DarkSeaGreen"); 91tcHeader[4].Text="福利"; 92tcHeader.Add(newTableHeaderCell()); 93tcHeader[5].Attributes.Add("bgcolor","LightSteelBlue"); 94tcHeader[5].Attributes.Add("colspan","2"); 95tcHeader[5].Text="联系方式 96 97//第三行表头 98tcHeader.Add(newTableHeaderCell()); 99tcHeader[6].Attributes.Add("bgcolor","Khaki"); 100tcHeader[6].Text="身份证号码"; 101tcHeader.Add(newTableHeaderCell()); 102tcHeader[7].Attributes.Add("bgcolor","Khaki"); 103tcHeader[7].Text="姓名"; 104tcHeader.Add(newTableHeaderCell()); 105tcHeader[8].Attributes.Add("bgcolor","Khaki"); 106tcHeader[8].Text="出生日期"; 107tcHeader.Add(newTableHeaderCell()); 108tcHeader[9].Attributes.Add("bgcolor","Khaki"); 109tcHeader[9].Text="薪水"; 110tcHeader.Add(newTableHeaderCell()); 111tcHeader[10].Attributes.Add("bgcolor","Khaki"); 112tcHeader[10].Text="家庭住址"; 113tcHeader.Add(newTableHeaderCell()); 114tcHeader[11].Attributes.Add("bgcolor","Khaki"); 115tcHeader[11].Text="邮政编码"; 116break; 117} 118} 119} 前台: 1 DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http: //www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 //www.w3.org/1999/xhtml"> 4 5 如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。 copyright@ 2008-2022 冰点文档网站版权所有 经营许可证编号:鄂ICP备2022015515号-1"; ";
冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。