GridView合并单元格(多列)
不知道合并gridview单元格有没有好的方法,是多列的。
比如我有姓名、照片、性别、出生年月等多列信息,其中这四列都是要合并单元格的。现在的问题就是姓名不太可能重复,但是照片会出现这样一个问题,因为可以不上传照片,所以如果相邻的两条信息都没有上传照片的话,照片这一列就不好合并了。性别的问题更突出,比如相邻的两条信息都是男或者都是女,合并好像也比较复杂。请问有没有好一点的办法?
或者说使用telerik 的radgrid可以实现这个功能吗?
原始样式姓名 照片 性别 出生年月 其它列1 其它列2 其它列3A P M XXXX O1 O2 O3A P M XXXX O4 O5 O6A P M XXXX O7 O8 O9B P2 F YYYY 10 11 12B P2 F YYYY 13 14 15B P2 F YYYY 16 17 18需求样式姓名 照片 性别 出生年月 其它列1 其它列2 其它列3 O1 O2 O3A P M XXXX O4 O5 O6 O7 O8 O9 10 11 12B P2 F YYYY 13 14 15 16 17 18
/// <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; } } } }
[解决办法]
这样的页面最好用table来做,然后ajax去后台取数据这样的灵活性大一点。
[解决办法]
一般都用Table 好操作
[解决办法]
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; } } }
[解决办法]
100分就是认识回答的人多啊
[解决办法]
在合并的过程中,有一些列需做些特殊的处理,如你提到的照片的情况,没上传,是空或null,在代码中判断出来,合并处理。
[解决办法]
repeater多么好多么方便 哎你说你````
[解决办法]
各位大侠,讨论结果是怎样的,哪种方法实现比较简单呢?
[解决办法]
8楼不可以?
<%@ 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>
[解决办法]
拼Table吧