62,041
社区成员
发帖
与我相关
我的任务
分享
原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A P M XXXX O1 O2 O3
A P M XXXX O4 O5 O6
A P M XXXX O7 O8 O9
B P2 F YYYY 10 11 12
B P2 F YYYY 13 14 15
B P2 F YYYY 16 17 18
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
O1 O2 O3
A P M XXXX O4 O5 O6
O7 O8 O9
10 11 12
B P2 F YYYY 13 14 15
16 17 18
<%@ Page Language="C#" %>
<script runat="server">
private void Page_Load()
{
List<Test> list = new List<Test>();
list.Add(new Test("a2", "b3", "c3"));
list.Add(new Test("a1", "b1", "c1"));
list.Add(new Test("a1", "b2", "c2"));
list.Add(new Test("a1", "b21", "c21"));
list.Add(new Test("a3", "b4", "c4"));
list.Add(new Test("a3", "b41", "c41"));
gridView.DataSource = list;
gridView.DataBind();
}
private string t1 = string.Empty;
private void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.DataRow:
Test test = e.Row.DataItem as Test;
if (t1 != test.a) t1 = string.Empty;
List<Test> list = gridView.DataSource as List<Test>;
int x = list.Where<Test>(delegate(Test obj) { return obj.a == test.a; }).Count();
if (x > 1)
{
if (t1 == string.Empty)
{
e.Row.Cells[0].Attributes.Add("rowspan", x.ToString());
t1 = test.a;
}
else {
e.Row.Cells.RemoveAt(0);
}
}
break;
}
}
private class Test
{
private string _a;
public string a { get { return _a; } set { _a = value; } }
private string _b;
public string b { get { return _b; } set { _b = value; } }
private string _c;
public string c { get { return _c; } set { _c = value; } }
public Test(string A, string B, string C)
{
_a = A;
_b = B;
_c = C;
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<asp:GridView ID="gridView" runat="server" OnRowDataBound="gridView_RowDataBound">
</asp:GridView>
</form>
</body>
</html>
原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A P M XXXX O1 O2 O3
A P M XXXX O4 O5 O6
A P M XXXX O7 O8 O9
A P M XXXX 10 11 12
---------------------------------------------
B P2 F YYYY 13 14 15
B P2 F YYYY 16 17 18
B P2 F YYYY 19 20 21
---------------------------------------------
C P2 F ZZZZ 22 23 24
C P2 F ZZZZ 25 26 27
C P2 F ZZZZ 28 29 30
C P2 F ZZZZ 31 32 33
C P2 F ZZZZ 34 35 36
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
O1 O2 O3
A P M XXXX O4 O5 O6
O7 O8 O9
10 11 12
----------------------------------------------
13 14 15
B P2 F YYYY 16 17 18
19 20 21
----------------------------------------------
22 23 24
25 26 27
C P2 F ZZZZ 28 29 30
31 32 33
34 35 36
原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A P M XXXX O1 O2 O3
A P M XXXX O4 O5 O6
A P M XXXX O7 O8 O9
A P M XXXX 10 11 12
---------------------------------------------
B P2 F YYYY 13 14 15
B P2 F YYYY 16 17 18
B P2 F YYYY 19 20 21
---------------------------------------------
C P2 F ZZZZ 22 23 24
C P2 F ZZZZ 25 26 27
C P2 F ZZZZ 28 29 30
C P2 F ZZZZ 31 32 33
C P2 F ZZZZ 34 35 36
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
O1 O2 O3
A P M XXXX O4 O5 O6
O7 O8 O9
10 11 12
----------------------------------------------
13 14 15
B P2 F YYYY 16 17 18
19 20 21
----------------------------------------------
22 23 24
25 26 27
C P2 F ZZZZ 28 29 30
31 32 33
34 35 36
protected void Page_Load(object sender, EventArgs e)
{
//...
GridView1.DataSource = dt;
GridView1.DataBind();
GroupName(0);
GroupName(1);
GroupName(3);
GroupSex();
}
public void GroupName(int col)
{
TableCell oldName = GridView1.Rows[0].Cells[col];
for (int i = 1; i < GridView1.Rows.Count; i++)
{
TableCell Name = GridView1.Rows[i].Cells[col];
if (oldName.Text == Name.Text)
{
Name.Visible = false;
if (oldName.RowSpan == 0)
{
oldName.RowSpan = 1;
}
oldName.RowSpan++;
oldName.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldName = Name;
}
}
}
public void GroupSex()
{
TableCell oldName = GridView1.Rows[0].Cells[0];
TableCell oldSex = GridView1.Rows[0].Cells[2];
for (int i = 1; i < GridView1.Rows.Count; i++)
{
TableCell Name = GridView1.Rows[i].Cells[0];
TableCell Sex = GridView1.Rows[i].Cells[2];
if (oldName.Text == Name.Text && oldSex.Text == Sex.Text)
{
Sex.Visible = false;
if (oldSex.RowSpan == 0)
{
oldSex.RowSpan = 1;
}
oldSex.RowSpan++;
oldSex.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldName = Name;
oldSex = Sex;
}
}
}
原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A P M XXXX O1 O2 O3
A P M XXXX O4 O5 O6
A P M XXXX O7 O8 O9
B P2 F YYYY 10 11 12
B P2 F YYYY 13 14 15
B P2 F YYYY 16 17 18
C P3 F ZZZZ 19 20 21
C P3 F ZZZZ 22 23 24
C P3 F ZZZZ 25 26 27
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
O1 O2 O3
A P M XXXX O4 O5 O6
O7 O8 O9
10 11 12
B P2 F YYYY 13 14 15
16 17 18
19 20 21
C P3 F ZZZZ 22 23 24
25 26 27
原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A P M XXXX O1 O2 O3
A P M XXXX O4 O5 O6
A P M XXXX O7 O8 O9
B P2 F YYYY 10 11 12
B P2 F YYYY 13 14 15
B P2 F YYYY 16 17 18
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
O1 O2 O3
A P M XXXX O4 O5 O6
O7 O8 O9
10 11 12
B P2 F YYYY 13 14 15
16 17 18
19 20 21
C P3 F ZZZZ 22 23 24
25 26 27
/// <summary>
/// 合并行(合并的列要用Label控件)
/// </summary>
/// <param name="gvw">需要合并的GridView</param>
/// <param name="sCol">要合并的列(从0开始)</param>
/// <param name="controlName">控件名称</param>
public static void MergeRows(GridView gvw, int col, string controlName)
{
for (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gvw.Rows[rowIndex];
GridViewRow previousRow = gvw.Rows[rowIndex + 1];
Label row_lbl = row.Cells[col].FindControl(controlName) as Label;
Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) as Label;
if (row_lbl != null && previousRow_lbl != null)
{
if (row_lbl.Text == previousRow_lbl.Text)
{
row.Cells[col].RowSpan = previousRow.Cells[col].RowSpan < 1 ? 2 : previousRow.Cells[col].RowSpan + 1;
previousRow.Cells[col].Visible = false;
}
}
}
}
<asp:GridView ID="gvUVSourceDistributionStatistic" runat="server" AutoGenerateColumns="false" CssClass="tableBorder"
onmouseover="changeto()" onmouseout="changeback()" Width="98%" HorizontalAlign="Center">
<Columns>
<asp:TemplateField HeaderText="日期" ItemStyle-HorizontalAlign="Center" HeaderStyle-Height="30"
ItemStyle-Height="25">
<ItemTemplate>
<asp:Label ID="lblSearchDate" runat="server" Text='<%# Convert.ToDateTime(Eval("SearchDate")).ToString("yyyy-MM-dd") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="访客来源" DataField="VisitorSource" ItemStyle-HorizontalAlign="Center"/>
</Columns>
</asp:GridView>