关于sql统计后显示的问题 如图一所示,此时查询得到的结果,怎么转为图二那种效果。行合并咋弄呢??如果在页面里面显示,该用什么显示比较好啊?? 请高手们发表意见。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 合并GridView中某列相同信息的行(单元格) /// </summary> /// <param name="GridView1">GridView</param> /// <param name="cellNum">第几列</param> public static void GroupRows(GridView gv, int cellNum) { int i = 0, rowSpanNum = 1; int j = 0; string[] _color = { "#eeeeee", "#ffffff" }; while (i < gv.Rows.Count - 1) { GridViewRow gvr = gv.Rows[i]; gvr.Attributes["bgcolor"] = _color[j % 2]; for (++i; i < gv.Rows.Count; i++) { GridViewRow gvrNext = gv.Rows[i]; if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text) { gvrNext.Attributes["bgcolor"] = _color[j % 2]; //gvrNext.Cells[cellNum].Visible = false; gvrNext.Cells[cellNum].Visible = false; rowSpanNum++; } else { gvr.Cells[cellNum].RowSpan = rowSpanNum; rowSpanNum = 1; j++; break; } if (i == gv.Rows.Count - 1) { gvr.Cells[cellNum].RowSpan = rowSpanNum; } } } } 使用方法: DataTable dt = new DataTable(); dt.Columns.Add("OrderID", typeof(int)); dt.Columns.Add("ProductID", typeof(int)); dt.Columns.Add("UnitPrice", typeof(float)); dt.Rows.Add(new object[] { 10248, 11, 14.0 }); dt.Rows.Add(new object[] { 10248, 42, 9.80 }); dt.Rows.Add(new object[] { 10248, 72, 34.8 }); dt.Rows.Add(new object[] { 10249, 14, 18.8 }); dt.Rows.Add(new object[] { 10249, 51, 42.4 }); dt.Rows.Add(new object[] { 10250, 41, 7.7 }); dt.Rows.Add(new object[] { 10250, 51, 42.4 }); dt.Rows.Add(new object[] { 10250, 65, 16.8 }); dt.Rows.Add(new object[] { 10251, 22, 16.8 }); dt.Rows.Add(new object[] { 10251, 57, 15.6 }); dt.Rows.Add(new object[] { 10251, 65, 15.6 }); GridView1.DataSource = dt; GridView1.DataBind(); GroupRows(GridView1, 0); 使用1楼的方法合并,但是还要判断合并的值出现相同的时候,要设置那些不显示的单元的visible的值为false。 比如 :第一行出现了 "一厂" ,设置它的RowSpan属性,判断下一个是不是还是一厂,如果是就要隐藏它的单元格。 public static void GroupRows(GridView gv, int[] CcellNum, int McellNum) { int i = 0, rowSpanNum = 1; int j = 0; string[] _color = { "#eeeeee", "#ffffff" }; while (i < gv.Rows.Count - 1) { GridViewRow gvr = gv.Rows[i]; gvr.Attributes["bgcolor"] = _color[j % 2]; for (++i; i < gv.Rows.Count; i++) { GridViewRow gvrNext = gv.Rows[i]; bool flang = true; for (int c = 0; c < CcellNum.Length; c++) { if (gvr.Cells[c].Text != gvrNext.Cells[c].Text) { flang = false; break; } } if (flang) { gvrNext.Attributes["bgcolor"] = _color[j % 2]; //gvrNext.Cells[cellNum].Visible = false; gvrNext.Cells[McellNum].Visible = false; rowSpanNum++; } else { gvr.Cells[McellNum].RowSpan = rowSpanNum; rowSpanNum = 1; j++; break; } if (i == gv.Rows.Count - 1) { gvr.Cells[McellNum].RowSpan = rowSpanNum; } } } } DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Value"); dt.Columns.Add("Num"); dt.Rows.Add(new object[] {"一厂","检查","22" }); dt.Rows.Add(new object[] { "一厂", "测试", "12" }); dt.Rows.Add(new object[] { "二厂", "测试", "22" }); dt.Rows.Add(new object[] { "二厂", "测试", "22" }); dt.Rows.Add(new object[] { "二厂", "检查", "22" }); GridView1.DataSource = dt; GridView1.DataBind(); GroupRows(GridView1, new int[] { 0 }, 0); GroupRows(GridView1, new int[] { 1, 2 }, 2); 好像不行啊,两次合并后 最后一列不合并啊(我这里是百分比了)。 GroupRowst(GridView1, new int[] { 0 }, 0); GroupRowst(GridView1, new int[] { 1, 5 }, 5); 你的"储运部"与"其他原因"不一样啊好像不行啊,两次合并后 最后一列不合并啊(我这里是百分比了)。 GroupRowst(GridView1, new int[] { 0 }, 0); GroupRowst(GridView1, new int[] { 1, 5 }, 5); 好像不行啊,两次合并后 最后一列不合并啊(我这里是百分比了)。 GroupRowst(GridView1, new int[] { 0 }, 0); GroupRowst(GridView1, new int[] { 1, 5 }, 5);但是他们都是属于二厂的啊 而且百分比也是根据这个厂来的啊 好像不行啊,两次合并后 最后一列不合并啊(我这里是百分比了)。 GroupRowst(GridView1, new int[] { 0 }, 0); GroupRowst(GridView1, new int[] { 1, 5 }, 5);但是他们都是属于二厂的啊 而且百分比也是根据这个厂来的啊 不然 这个总百分比 一算 结果不等于百分之百啊 如果两个厂一样,你直接比较5不就好了吗?GroupRowst(GridView1, new int[] { 0 }, 0);GroupRowst(GridView1, new int[] { 5 }, 5); 好像不行啊,两次合并后 最后一列不合并啊(我这里是百分比了)。 GroupRowst(GridView1, new int[] { 0 }, 0); GroupRowst(GridView1, new int[] { 1, 5 }, 5); 额貌似我弄错了 GroupRowst(GridView1, new int[] { 1, 5 }, 5); 但改为 GroupRowst(GridView1, new int[] { 0, 5 }, 5); 也不行啊 应该是GroupRowst(GridView1, new int[] { 0 }, 0);GroupRowst(GridView1, new int[] {0, 5 }, 5); 怎么写一个存储过程定期删除数据里的数据。 在asp里怎么用代码做“当前位置”这一模块。 防止IE阻止弹出窗口 躲在一般交换机后面的电脑如何做网站? DataGrid中如何处理多个DowpDownList级联响应? XMLHTTP 调用 Web Service 的问题? 谁能帮小女子把这ASP的代码改为ASP。NET的 多谢了! 实践性问题:高手指点 aspx页面,模板语言+repeater求救 手机注册收费问题 datagrid自带分页功能怎么做的? 如何将数据做成表格导出到Word文档,并将文档上传到服务器。
/// 合并GridView中某列相同信息的行(单元格)
/// </summary>
/// <param name="GridView1">GridView</param>
/// <param name="cellNum">第几列</param>
public static void GroupRows(GridView gv, int cellNum)
{
int i = 0, rowSpanNum = 1;
int j = 0;
string[] _color = { "#eeeeee", "#ffffff" };
while (i < gv.Rows.Count - 1)
{
GridViewRow gvr = gv.Rows[i];
gvr.Attributes["bgcolor"] = _color[j % 2];
for (++i; i < gv.Rows.Count; i++)
{
GridViewRow gvrNext = gv.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Attributes["bgcolor"] = _color[j % 2];
//gvrNext.Cells[cellNum].Visible = false;
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
j++;
break;
}
if (i == gv.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
DataTable dt = new DataTable();
dt.Columns.Add("OrderID", typeof(int));
dt.Columns.Add("ProductID", typeof(int));
dt.Columns.Add("UnitPrice", typeof(float)); dt.Rows.Add(new object[] { 10248, 11, 14.0 });
dt.Rows.Add(new object[] { 10248, 42, 9.80 });
dt.Rows.Add(new object[] { 10248, 72, 34.8 });
dt.Rows.Add(new object[] { 10249, 14, 18.8 });
dt.Rows.Add(new object[] { 10249, 51, 42.4 });
dt.Rows.Add(new object[] { 10250, 41, 7.7 });
dt.Rows.Add(new object[] { 10250, 51, 42.4 });
dt.Rows.Add(new object[] { 10250, 65, 16.8 });
dt.Rows.Add(new object[] { 10251, 22, 16.8 });
dt.Rows.Add(new object[] { 10251, 57, 15.6 });
dt.Rows.Add(new object[] { 10251, 65, 15.6 });
GridView1.DataSource = dt;
GridView1.DataBind(); GroupRows(GridView1, 0);
{
int i = 0, rowSpanNum = 1;
int j = 0;
string[] _color = { "#eeeeee", "#ffffff" };
while (i < gv.Rows.Count - 1)
{
GridViewRow gvr = gv.Rows[i];
gvr.Attributes["bgcolor"] = _color[j % 2];
for (++i; i < gv.Rows.Count; i++)
{
GridViewRow gvrNext = gv.Rows[i];
bool flang = true;
for (int c = 0; c < CcellNum.Length; c++)
{
if (gvr.Cells[c].Text != gvrNext.Cells[c].Text)
{
flang = false;
break;
}
}
if (flang)
{
gvrNext.Attributes["bgcolor"] = _color[j % 2];
//gvrNext.Cells[cellNum].Visible = false;
gvrNext.Cells[McellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[McellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
j++;
break;
}
if (i == gv.Rows.Count - 1)
{
gvr.Cells[McellNum].RowSpan = rowSpanNum;
}
}
}
} DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Value");
dt.Columns.Add("Num"); dt.Rows.Add(new object[] {"一厂","检查","22" });
dt.Rows.Add(new object[] { "一厂", "测试", "12" });
dt.Rows.Add(new object[] { "二厂", "测试", "22" });
dt.Rows.Add(new object[] { "二厂", "测试", "22" });
dt.Rows.Add(new object[] { "二厂", "检查", "22" });
GridView1.DataSource = dt;
GridView1.DataBind(); GroupRows(GridView1, new int[] { 0 }, 0);
GroupRows(GridView1, new int[] { 1, 2 }, 2);
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] { 1, 5 }, 5);
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] { 1, 5 }, 5);
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] { 1, 5 }, 5);但是他们都是属于二厂的啊 而且百分比也是根据这个厂来的啊
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] { 1, 5 }, 5);但是他们都是属于二厂的啊 而且百分比也是根据这个厂来的啊 不然 这个总百分比 一算 结果不等于百分之百啊
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] { 5 }, 5);
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] { 1, 5 }, 5); 额貌似我弄错了 GroupRowst(GridView1, new int[] { 1, 5 }, 5); 但改为 GroupRowst(GridView1, new int[] { 0, 5 }, 5); 也不行啊
GroupRowst(GridView1, new int[] { 0 }, 0);
GroupRowst(GridView1, new int[] {0, 5 }, 5);