表格生成的

chenhaoying 2009-05-07 10:40:04
有个数据库表的格式是这样的:
A B
1 01
1 02
1 03
2 02
2 03
2 04
2 05
3 01
3 02

如何生成二维表格如下:
101 102 103
202 203 204 205
301 302
...全文
82 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhulong1111 2009-05-07
  • 打赏
  • 举报
回复
这个简单撒 你先根据ID 查出来 在后台写个方法得到想102这种数据的集合,在Table的tr里循环遍历这集合,在多遍历下tr 就又这效果了 虽然过程可能复杂点 但比直接绑定什么控件的 数度快得多!
中年秃头大叔 2009-05-07
  • 打赏
  • 举报
回复
希望高手帮一忙,学习一下...
zzxap 2009-05-07
  • 打赏
  • 举报
回复
ojlovecd真有耐心写啊
chenhaoying 2009-05-07
  • 打赏
  • 举报
回复
若数据是
A B
1 a
1 b
1 c
2 a
2 c
2 d
2 e
3 a
3 b

这样的呢?
yh7272hy 2009-05-07
  • 打赏
  • 举报
回复
3楼的方法好
bobui 2009-05-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ojlovecd 的回复:]
参考:

C# code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("A", typeof(int)), new DataColumn("B", typeof(string)) });
dt.Rows.Add(1, "01");
dt.Rows.Add(1, "02");

[/Quote]
赞同这种做法
chenhaoying 2009-05-07
  • 打赏
  • 举报
回复
现在取出后的数据不会对应放。比如102,202,302必须是在同一列的,而没有的则保留空格,大于的则往后放。
就是这样的
101 102 103
202 203 204 205
301 302 303
我姓区不姓区 2009-05-07
  • 打赏
  • 举报
回复
参考:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("A", typeof(int)), new DataColumn("B", typeof(string)) });
dt.Rows.Add(1, "01");
dt.Rows.Add(1, "02");
dt.Rows.Add(1, "03");
dt.Rows.Add(2, "02");
dt.Rows.Add(2, "03");
dt.Rows.Add(2, "04");
dt.Rows.Add(2, "05");
dt.Rows.Add(3, "01");
dt.Rows.Add(3, "02");
Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>();
int max = 0;
foreach (DataRow dr in dt.Rows)
{
int a = Convert.ToInt32(dr["A"]);
int b = Convert.ToInt32(dr["B"]);
if (b > max)
max = b;
if (dic.ContainsKey(a))
{
dic[a].Add(b);
}
else
{
List<int> list = new List<int>();
list.Add(b);
dic.Add(a, list);
}
}
System.Web.UI.HtmlControls.HtmlTable table = new System.Web.UI.HtmlControls.HtmlTable();
for (int i = 0; i < dic.Count; i++)
{
System.Web.UI.HtmlControls.HtmlTableRow row = new System.Web.UI.HtmlControls.HtmlTableRow();
for (int j = 0; j < max; j++)
{
System.Web.UI.HtmlControls.HtmlTableCell cell = new System.Web.UI.HtmlControls.HtmlTableCell();
List<int> list = dic[i + 1];
if (list.Contains(j + 1))
cell.InnerText = (i + 1) + (j + 1).ToString("00");
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
this.form1.Controls.Add(table);
}
}

chenyu112 2009-05-07
  • 打赏
  • 举报
回复
同意楼上
zzxap 2009-05-07
  • 打赏
  • 举报
回复
select A+B as ab from table

然后绑定到repeater就行了
cpp2017 2009-05-07
  • 打赏
  • 举报
回复
字段数不固定,所以sql无法做,在界面中做
可以使用repeater嵌套


主repeater绑定 select distinct a from 表


副表绑定对应A
下面的B

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧