C# 在asp.net中填充动态表?
我非常感谢你在这方面的帮助。这是我的问题: 我有一个产品对象的ArrayList。每个产品都有一个id、名称和一个供应商。 当我迭代arraylist并创建一个表将该值放入单元格时,我遇到了一个问题,即一个产品可以有多个供应商。如果是这种情况,则id和名称相同,但arraylist中的供应商不同。 到目前为止,我的代码通过为id和name创建空单元格并将另一个供应商放在一个新单元格中来解决这个问题 但是,为每个供应商创建新行看起来并不好。我想要的是,如果一个产品有多个供应商,我希望所有供应商都在同一行的同一单元格中,作为产品idC# 在asp.net中填充动态表?,c#,asp.net,dynamic,C#,Asp.net,Dynamic,我非常感谢你在这方面的帮助。这是我的问题: 我有一个产品对象的ArrayList。每个产品都有一个id、名称和一个供应商。 当我迭代arraylist并创建一个表将该值放入单元格时,我遇到了一个问题,即一个产品可以有多个供应商。如果是这种情况,则id和名称相同,但arraylist中的供应商不同。 到目前为止,我的代码通过为id和name创建空单元格并将另一个供应商放在一个新单元格中来解决这个问题 但是,为每个供应商创建新行看起来并不好。我想要的是,如果一个产品有多个供应商,我希望所有供应商都在
string id = string.Empty;
int count = 0;
public void CreateResultTable(ArrayList result)
{
TableRow row;
TableCell cell;
if (result != null && result.Count > 0)
{
foreach (Item item in result)
{
if (count == 0)
{
row = new TableRow();
id = item.id;
cell = new TableCell();
cell.Text = item.id;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = item.product;
row.Cells.Add(cell);
cell = new TableCell();
ArrayList levList = item.suppliers;
if (levList != null)
{
string lev = string.Empty;
for (int i = 0; i < levList.Count; i++)
{
lev += levList[i];
}
cell.Text = lev;
row.Cells.Add(cell);
}
else
cell.Text = string.Empty;
row.Cells.Add(cell);
count++;
}
else if (id != item.id)
{
row = new TableRow();
id = item.id;
cell = new TableCell();
cell.Text = item.id;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = item.product;
row.Cells.Add(cell);
cell = new TableCell();
ArrayList levList = item.suppliers;
if (levList != null)
{
string lev = string.Empty;
for (int i = 0; i < levList.Count; i++)
{
lev += levList[i];
}
cell.Text = lev;
}
else
cell.Text = string.Empty;
row.Cells.Add(cell);
}
else
{
row = new TableRow();
cell = new TableCell();
cell.Text = string.Empty;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = string.Empty;
row.Cells.Add(cell);
cell = new TableCell();
ArrayList levList = item.suppliers;
if (levList != null)
{
string lev = string.Empty;
for (int i = 0; i < levList.Count; i++)
{
lev += levList[i];
}
cell.Text = lev;
row.Cells.Add(cell);
}
else
cell.Text = string.Empty;
row.Cells.Add(cell);
}
SearchResultLev.Rows.Add(row);
}
SearchResultLev.Visible = true;
SearchResult.Visible = false;
NoSearchResult.Visible = false;
}
else
{
SearchResultLev.Visible = false;
SearchResult.Visible = false;
NoSearchResult.Visible = true;
}
}
string id=string.Empty;
整数计数=0;
public void createResultable(ArrayList结果)
{
表行;
表细胞;
if(result!=null&&result.Count>0)
{
foreach(结果中的项目)
{
如果(计数=0)
{
行=新表行();
id=item.id;
单元格=新表单元格();
cell.Text=item.id;
行.单元格.添加(单元格);
单元格=新表单元格();
cell.Text=item.product;
行.单元格.添加(单元格);
单元格=新表单元格();
ArrayList=item.suppliers;
if(levList!=null)
{
string lev=string.Empty;
for(int i=0;i
您可以在此处使用哈希表:
Hashtable htProductCell = new Hashtable();
if (!htProductCell.Contains(item.product))
{
//add a new row for the item
htProductCell.Add(item.product, cell);
}
else
{
TableCell cell = (TableCell)htProductCell[item.product];
ArrayList levList = item.suppliers;
if (levList != null)
{
string lev = string.Empty;
for (int i = 0; i < levList.Count; i++)
{
lev += levList[i];
}
cell.Text += lev;
row.Cells.Add(cell);
}
}
Hashtable htProductCell=new Hashtable();
如果(!htProductCell.Contains(item.product))
{
//为项目添加新行
htProductCell.Add(item.product,cell);
}
其他的
{
TableCell单元格=(TableCell)htProductCell[item.product];
ArrayList=item.suppliers;
if(levList!=null)
{
string lev=string.Empty;
for(int i=0;i
使用GridView,而不是在代码隐藏中生成表。
我这里有一个示例,它在GridView的项模板中使用GridView和一个转发器。
中继器为每个供应商列出一个无限的列表
标记:
<asp:GridView ID='GridView1' runat='server' AutoGenerateColumns='false'>
<Columns>
<asp:BoundField HeaderText='Product ID' DataField='ID' />
<asp:BoundField HeaderText='Name' DataField='Name' />
<asp:TemplateField HeaderText='Suppliers'>
<ItemTemplate>
<asp:Repeater DataSource='<%# Bind("Suppliers") %>' runat="server" ID='Repeater1'>
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Name") %></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和绑定数据的代码(类型定义如下)
GridView1.DataSource=新列表
{
新测试产品
{
Name=“Test”,
ID=“1”,
供应商=新名单
{
新的测试供应商{Name=“Supplier1”},
新的测试供应商{Name=“Supplier2”},
新测试供应商{Name=“非常长的供应商名称”}
}
}
};
GridView1.DataBind();
我使用了样本TestProduct和TestSuppliers
public class TestProduct
{
public String ID { get; set; }
public String Name { get; set; }
public List<TestSupplier> Suppliers { get; set; }
}
public class TestSupplier { public String Name { get; set; }}
公共类测试产品
{
public class TestProduct
{
public String ID { get; set; }
public String Name { get; set; }
public List<TestSupplier> Suppliers { get; set; }
}
public class TestSupplier { public String Name { get; set; }}