关于GridView的横向合并数据信息
Posted lysouko
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于GridView的横向合并数据信息相关的知识,希望对你有一定的参考价值。
此为asp.net
前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="Web.WebForm4" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> .model_s { width: 90px; } .model_er { width:50px; } .GridViewStyle { border-right: 2px solid #A7A6AA; border-bottom: 2px solid #A7A6AA; border-left: 2px solid white; border-top: 2px solid white; padding: 4px; } .GridViewStyle a { color:#0026ff; } .GridViewHeaderStyle th { border-left: 1px solid #000000; border-right: 1px solid #000000; } .GridViewHeaderStyle { background-color: #5D7B9D; font-weight: bold; color: White; } .GridViewFooterStyle { background-color: #5D7B9D; /*background-color: #b6ff00;*/ font-weight: bold; color: White; } .GridViewRowStyle { background-color: #F7F6F3; color: #333333; } .GridViewAlternatingRowStyle { background-color: #FFFFFF; color: #284775; } .GridViewRowStyle td, .GridViewAlternatingRowStyle td { border: 1px solid #000000; } .GridViewSelectedRowStyle { background-color: #E2DED6; font-weight: bold; color: #333333; } .GridViewPagerStyle { background-color: #284775; color: #FFFFFF; } .GridViewPagerStyle table /* to center the paging links*/ { margin: 0 auto 0 auto; } #set_ms { width: 1000px; border: 0px solid #000000; margin: 0 auto; } #sss { width: 900px; border: 1px solid #000000; margin: 100px auto; } .modd { float:right; } .user_s { display:none; } </style> </head> <body> <form id="form1" runat="server"> <div id="sss" style="display:block"> <asp:GridView ID="GrvInfo1" runat="server" AutoGenerateColumns="False" OnDataBound="GrvInfo1_DataBound" OnRowCreated="GrvInfo1_RowCreated" CssClass="GridViewStyle" OnRowDataBound="GrvInfo1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="序号" SortExpression="stu1" ItemStyle-CssClass="model_s"> <ItemTemplate> <asp:Label ID="lblstu1" runat="server" Text=‘<%# Eval("stu1") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="组别" SortExpression="stu2" ItemStyle-CssClass="model_s"> <ItemTemplate> <asp:Label ID="lblstu2" runat="server" Text=‘<%# Eval("stu2") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="姓名" SortExpression="stu3" ItemStyle-CssClass="model_s"> <ItemTemplate> <asp:Label ID="lblstu3" runat="server" Text=‘<%# Eval("stu3") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="一" SortExpression="stu4_1"> <ItemTemplate> <asp:Label ID="lblstu4_1" runat="server" Text=‘<%# Eval("stu4_1") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="二" SortExpression="stu4_2"> <ItemTemplate> <asp:Label ID="lblstu4_2" runat="server" Text=‘<%# Eval("stu4_2") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="三" SortExpression="stu4_3"> <ItemTemplate> <asp:Label ID="lblstu4_3" runat="server" Text=‘<%# Eval("stu4_3") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="四" SortExpression="stu4_4"> <ItemTemplate> <asp:Label ID="lblstu4_4" runat="server" Text=‘<%# Eval("stu4_4") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="五" SortExpression="stu4_5"> <ItemTemplate> <asp:Label ID="lblstu4_5" runat="server" Text=‘<%# Eval("stu4_5") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="六" SortExpression="stu4_6" ItemStyle-CssClass="model_s"> <ItemTemplate> <asp:Label ID="lblstu4_6" runat="server" Text=‘<%# Eval("stu4_6") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField HeaderText="日" SortExpression="stu4_7" ItemStyle-CssClass="model_s"> <ItemTemplate> <asp:Label ID="lblstu4_7" runat="server" Text=‘<%# Eval("stu4_7") %>‘></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> </Columns> <FooterStyle CssClass="GridViewFooterStyle" /> <RowStyle CssClass="GridViewRowStyle" /> <SelectedRowStyle CssClass="GridViewSelectedRowStyle" /> <PagerStyle CssClass="GridViewPagerStyle" /> <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" /> <HeaderStyle CssClass="GridViewHeaderStyle" /> </asp:GridView> </div> </form> <script type="text/javascript"> var _oldColor; function SetNewColor(source) { _oldColor = source.style.backgroundColor; source.style.backgroundColor = ‘#999999‘; } function SetOldColor(source) { source.style.backgroundColor = _oldColor; } </script> </body> </html>
后端代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GrvInfo1.DataSource = new List<ste_ff>() { new ste_ff(){stu1="1",stu2="南区部",stu3="纪文义",stu4_1="牵头南区工作",stu4_2="牵头南区工作",stu4_3="牵头南区工作",stu4_4="牵头南区工作",stu4_5="牵头南区工作",stu4_6="休息",stu4_7="休息" }, new ste_ff(){stu1="2",stu2="南区部",stu3="纪文义1",stu4_1="水泵状态建设",stu4_2="水泵状态建设",stu4_3="水泵状态建设",stu4_4="付款督办",stu4_5="付款督办",stu4_6="休息",stu4_7="休息"}, new ste_ff(){stu1="3",stu2="中区部",stu3="纪文义2",stu4_1="双月会准备1",stu4_2="双月会准备2",stu4_3="双月会准备2",stu4_4="制度文库修订汇报",stu4_5="制度文库修订汇报",stu4_6="休息",stu4_7="休息"}, new ste_ff(){stu1="4",stu2="中区部",stu3="纪文义3",stu4_1="培训课件审核汇报",stu4_2="培训课件审核汇报",stu4_3="培训课件审核汇报",stu4_4="培训课件审核汇报",stu4_5="培训课件审核汇报",stu4_6="休息",stu4_7="休息"}, new ste_ff(){stu1="5",stu2="北区部",stu3="纪文义4",stu4_1="牵头南区工作1",stu4_2="牵头南区工作1",stu4_3="牵头南区工作",stu4_4="牵头南区工作",stu4_5="牵头南区工作",stu4_6="休息",stu4_7="休息"}, }; this.GrvInfo1.DataBind(); } } #region 下周计划 protected void GrvInfo1_DataBound(object sender, EventArgs e) { //合并行 CommonClass_s.UnitRow_s(GrvInfo1, 1, "lblstu2"); //合并列 CommonClass_s.UnitCell_s(GrvInfo1, 3, "lblstu4_1", "lblstu4_2", "lblstu4_3", "lblstu4_4", "lblstu4_5"); } public class CommonClass_s { #region 合并行(相同值)-普通列 /// <summary> /// 合并行(普通列) /// </summary> /// <param name=“gv”>所对应的GridView对象</param> /// <param name=“columnIndex”>所对应要合并的列的索引</param> public static void UnitRow_s(GridView gv, int columnIndex) { int i; string lastType; int lastCell; if (gv.Rows.Count > 0) { lastType = gv.Rows[0].Cells[columnIndex].Text; gv.Rows[0].Cells[columnIndex].RowSpan = 1; lastCell = 0; for (i = 1; i < gv.Rows.Count; i++) { if (gv.Rows[i].Cells[columnIndex].Text == lastType) { gv.Rows[i].Cells[columnIndex].Visible = false; gv.Rows[lastCell].Cells[columnIndex].RowSpan++; } else { lastType = gv.Rows[i].Cells[columnIndex].Text; lastCell = i; gv.Rows[i].Cells[columnIndex].RowSpan = 1; } } } } #endregion #region 合并行(相同值)-模板列 /// <summary> /// 合并行(模板列) /// </summary> /// <param name=“gv”>所对应的GridView对象</param> /// <param name=“columnIndex”>所对应要合并的列的索引</param> /// <param name=“lblName”>模板列里面Lable的Id</param> public static void UnitRow_s(GridView gv, int columnIndex, string lblName) { int i; string lastType; int lastCell; if (gv.Rows.Count > 0) { lastType = (gv.Rows[0].Cells[columnIndex].FindControl(lblName) as Label).Text; gv.Rows[0].Cells[columnIndex].RowSpan = 1; lastCell = 0; for (i = 1; i < gv.Rows.Count; i++) { if ((gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text == lastType) { gv.Rows[i].Cells[columnIndex].Visible = false; gv.Rows[lastCell].Cells[columnIndex].RowSpan++; } else { lastType = (gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text; lastCell = i; gv.Rows[i].Cells[columnIndex].RowSpan = 1; } } } } #endregion #region 合并列(相同值)-普通列 /// <summary> /// 合并列(普通列) /// </summary> /// <param name="gv">所对应的GridView对象</param> /// <param name="columnIndex">所对应要合并的列的索引</param> public static void UnitCell_s(GridView gv, int columnIndex) { int i; string lastType; if (gv.Rows.Count > 0) { for (i = 1; i < gv.Rows.Count; i++) { lastType = gv.Rows[i].Cells[columnIndex].Text; gv.Rows[i].Cells[columnIndex].ColumnSpan = 1; if (gv.Rows[i].Cells[columnIndex + 1].Text == lastType) { gv.Rows[i].Cells[columnIndex + 1].Visible = false; gv.Rows[i].Cells[columnIndex].ColumnSpan++; } else { lastType = gv.Rows[i].Cells[columnIndex].Text; gv.Rows[i].Cells[columnIndex].ColumnSpan = 1; } } } } #endregion #region 合并列(相同值)-模板列 /// <summary> /// 合并行(模板列) /// </summary> /// <param name=“gv”>所对应的GridView对象</param> /// <param name=“columnIndex”>所对应要合并的列的索引</param> /// <param name=“lblName1”>模板列里面Lable1的Id</param> /// /// <param name=“lblName1”>模板列里面Lable2的Id</param> public static void UnitCell_s(GridView gv, int columnIndex, string lblName1, string lblName2, string lblName3, string lblName4, string lblName5) { int i; string lastType1; string lastType2; string lastType3; string lastType4; string lastType5; int num_set = 90;//单表格列的列宽 if (gv.Rows.Count > 0) { for (i = 0; i < gv.Rows.Count; i++) { int models = 1; int mnw = 0; lastType1 = (gv.Rows[i].Cells[columnIndex].FindControl(lblName1) as Label).Text; lastType2 = (gv.Rows[i].Cells[columnIndex + 1].FindControl(lblName2) as Label).Text; lastType3 = (gv.Rows[i].Cells[columnIndex + 2].FindControl(lblName3) as Label).Text; lastType4 = (gv.Rows[i].Cells[columnIndex + 3].FindControl(lblName4) as Label).Text; lastType5 = (gv.Rows[i].Cells[columnIndex + 4].FindControl(lblName5) as Label).Text; string model_r = lastType1 + "," + lastType2 + "," + lastType3 + "," + lastType4 + "," + lastType5; string[] model_uu = model_r.Trim().Split(‘,‘); gv.Rows[i].Cells[columnIndex].ColumnSpan = 1; ///=================== if (lastType1 == lastType2) { gv.Rows[i].Cells[columnIndex + 1].Visible = false; gv.Rows[i].Cells[columnIndex].ColumnSpan = 2; gv.Columns[columnIndex].ItemStyle.Width = num_set * 1; } if (lastType2 == lastType3) { gv.Rows[i].Cells[columnIndex + 2].Visible = false; if (lastType1 == lastType3) { gv.Rows[i].Cells[columnIndex].ColumnSpan = 3; gv.Columns[columnIndex].ItemStyle.Width = num_set * 3; } else { gv.Rows[i].Cells[columnIndex + 1].ColumnSpan = 2; gv.Columns[columnIndex + 1].ItemStyle.Width = num_set * 2; } } if (lastType3 == lastType4) { gv.Rows[i].Cells[columnIndex + 3].Visible = false; if (lastType1 == lastType4) { gv.Rows[i].Cells[columnIndex].ColumnSpan = 4; gv.Columns[columnIndex].ItemStyle.Width = num_set * 4; } else { if (lastType2 == lastType4) { gv.Rows[i].Cells[columnIndex + 1].ColumnSpan = 3; gv.Columns[columnIndex + 1].ItemStyle.Width = num_set * 3; } else { gv.Rows[i].Cells[columnIndex + 2].ColumnSpan = 2; gv.Columns[columnIndex + 2].ItemStyle.Width = num_set * 2; } } } if (lastType4 == lastType5) { gv.Rows[i].Cells[columnIndex + 4].Visible = false; if (lastType1 == lastType5) { gv.Rows[i].Cells[columnIndex].ColumnSpan = 5; gv.Columns[columnIndex].ItemStyle.Width = num_set * 5; } else { if (lastType2 == lastType5) { gv.Rows[i].Cells[columnIndex + 1].ColumnSpan = 4; gv.Columns[columnIndex + 1].ItemStyle.Width = num_set * 4; } else { if (lastType3 == lastType5) { gv.Rows[i].Cells[columnIndex + 2].ColumnSpan = 3; gv.Columns[columnIndex + 2].ItemStyle.Width = num_set * 3; } else { gv.Rows[i].Cells[columnIndex + 3].ColumnSpan = 2; gv.Columns[columnIndex + 3].ItemStyle.Width = num_set * 2; } } } } } } } #endregion } protected void GrvInfo1_RowCreated(object sender, GridViewRowEventArgs e) { //获取下周所有日期 #region 对获取下周日期的特殊转换(获取下周一时间) DateTime currentTime = DateTime.Now; int week = Convert.ToInt32(currentTime.DayOfWeek); week = week == 0 ? 7 : week; var start_time_next_week = currentTime.AddDays(1 - week + 7);//下周星期一 string set_time = start_time_next_week.ToString("yyyy-MM-dd"); #endregion string[] set_num = set_time.Trim().Split(‘-‘); int Year_s = 0;//年 int Month_s = 0;//月 int Day_s = 0;//日 Year_s = Convert.ToInt32(set_num[0]); Month_s = Convert.ToInt32(set_num[1]); Day_s = Convert.ToInt32(set_num[2]); DateTime someDay = new DateTime(Year_s, Month_s, Day_s); //此处换成你实际的日期 int wd = (int)someDay.DayOfWeek; string date_time1 = null;//用于接收日期字符 for (int i = 1 - wd; i < 8 - wd; i++) { DateTime currentDay = someDay.AddDays(i); DateTime dt = DateTime.Parse(currentDay.ToShortDateString()); date_time1 += dt.Day.ToString() + ","; } date_time1 = date_time1.Remove(date_time1.Length - 1, 1); string[] date_time2 = date_time1.Trim().Split(‘,‘); switch (e.Row.RowType) { case DataControlRowType.Header://行是标题行 TableCellCollection personHeader = e.Row.Cells;//标题行的单元格集合 personHeader.Clear();//清空 //添加一个表头 比如以及表头有三列 那么序号就是 0 1 2 personHeader.Add(new TableHeaderCell()); personHeader[0].Attributes.Add("rowspan", "2"); //跨2行 personHeader[0].Attributes.Add("colspan", "1"); //跨1列 personHeader[0].Attributes.Add("bgcolor", "DarkGreen"); personHeader[0].Text = "序号"; personHeader.Add(new TableHeaderCell()); personHeader[1].Attributes.Add("rowspan", "2"); //跨2行 personHeader[1].Attributes.Add("colspan", "1"); //跨1列 personHeader[1].Attributes.Add("bgcolor", "DarkGreen"); personHeader[1].Text = "组别"; personHeader.Add(new TableHeaderCell()); personHeader[2].Attributes.Add("rowspan", "2"); //跨2行 personHeader[2].Attributes.Add("colspan", "1"); //跨1列 personHeader[2].Attributes.Add("bgcolor", "DarkGreen"); personHeader[2].Text = "姓名"; personHeader.Add(new TableHeaderCell()); personHeader[3].Attributes.Add("colspan", "1"); //跨3列 personHeader[3].Attributes.Add("bgcolor", "green"); personHeader[3].Text = "" + date_time2[0] + ""; personHeader.Add(new TableHeaderCell()); personHeader[4].Attributes.Add("colspan", "1"); //跨3列 personHeader[4].Attributes.Add("bgcolor", "green"); personHeader[4].Text = "" + date_time2[1] + ""; personHeader.Add(new TableHeaderCell()); personHeader[5].Attributes.Add("colspan", "1"); //跨3列 personHeader[5].Attributes.Add("bgcolor", "green"); personHeader[5].Text = "" + date_time2[2] + ""; personHeader.Add(new TableHeaderCell()); personHeader[6].Attributes.Add("colspan", "1"); //跨3列 personHeader[6].Attributes.Add("bgcolor", "green"); personHeader[6].Text = "" + date_time2[3] + ""; personHeader.Add(new TableHeaderCell()); personHeader[7].Attributes.Add("colspan", "1"); //跨3列 personHeader[7].Attributes.Add("bgcolor", "green"); personHeader[7].Text = "" + date_time2[4] + ""; personHeader.Add(new TableHeaderCell()); personHeader[8].Attributes.Add("colspan", "1"); //跨3列 personHeader[8].Attributes.Add("bgcolor", "green"); personHeader[8].Text = "" + date_time2[5] + ""; personHeader.Add(new TableHeaderCell()); personHeader[9].Attributes.Add("colspan", "1"); //跨3列 personHeader[9].Attributes.Add("bgcolor", "green"); personHeader[9].Text = "" + date_time2[6] + "</th></tr><tr>"; //再添加一个表头 personHeader.Add(new TableHeaderCell()); personHeader[10].Attributes.Add("bgcolor", "Khaki"); personHeader[10].Text = "一"; personHeader.Add(new TableHeaderCell()); personHeader[11].Attributes.Add("bgcolor", "Khaki"); personHeader[11].Text = "二"; personHeader.Add(new TableHeaderCell()); personHeader[12].Attributes.Add("bgcolor", "Khaki"); personHeader[12].Text = "三"; personHeader.Add(new TableHeaderCell()); personHeader[13].Attributes.Add("bgcolor", "Khaki"); personHeader[13].Text = "四"; personHeader.Add(new TableHeaderCell()); personHeader[14].Attributes.Add("bgcolor", "Khaki"); personHeader[14].Text = "五"; personHeader.Add(new TableHeaderCell()); personHeader[15].Attributes.Add("bgcolor", "Khaki"); personHeader[15].Text = "六"; personHeader.Add(new TableHeaderCell()); personHeader[16].Attributes.Add("bgcolor", "Khaki"); personHeader[16].Text = "日"; //还可以继续添加 //记住不论多少行表头,每个列的序号是一次递增的,而且需要换行 —</th></tr><tr> break; } } #endregion
行颜色渐变
protected void GrvInfo1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);"); e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);"); } }
ste_ff类:
public class ste_ff { public string stu1 { set; get; } public string stu1_1 { set; get; } public string stu2 { set; get; } public string stu3 { set; get; } public string stu4_1 { set; get; } public string stu4_2 { set; get; } public string stu4_3 { set; get; } public string stu4_4 { set; get; } public string stu4_5 { set; get; } public string stu4_6 { set; get; } public string stu4_7 { set; get; } public string stu5_1 { set; get; } public string stu5_2 { set; get; } public string stu5_3 { set; get; } public string stu5_4 { set; get; } public string stu5_5 { set; get; } public string stu5_6 { set; get; } public string stu5_7 { set; get; } /// <summary>用于接收星期一ID</summary> public string stu_ID1 { set; get; } /// <summary>用于接收星期二ID</summary> public string stu_ID2 { set; get; } /// <summary>用于接收星期三ID</summary> public string stu_ID3 { set; get; } /// <summary>用于接收星期四ID</summary> public string stu_ID4 { set; get; } /// <summary>用于接收星期五ID</summary> public string stu_ID5 { set; get; } /// <summary>用于接收星期六ID</summary> public string stu_ID6 { set; get; } /// <summary>用于接收星期七ID</summary> public string stu_ID7 { set; get; } }
运行展示:
以上是关于关于GridView的横向合并数据信息的主要内容,如果未能解决你的问题,请参考以下文章