在asp.net中填充动态表?

发布于 2024-10-11 20:37:35 字数 3552 浏览 1 评论 0原文

我非常感谢您对此的帮助。这是我的问题:

我有一个产品对象的 ArrayList。每个产品都有一个 ID、名称和供应商。 当我迭代数组列表并创建一个表以将该值放入单元格时,我遇到了一个问题:一种产品可以有多个供应商。如果是这种情况,则 id 和名称相同,但数组列表中的供应商不同。 到目前为止,我的代码通过为 id 和 name 创建空单元格并将其他供应商放入新单元格来解决此问题。

但是,为每个供应商创建新行看起来并不好。我想要的是,如果一种产品有多个供应商,我希望所有供应商都位于产品 ID 行的同一单元格中。

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;
    }

}

I would really appreciate your help on this. This is my problem:

I have a ArrayList of product objects. Each product has a id, name and a supplier.
When I iterate the arraylist and creates a table to put this value into cells I come to the problem that a product can have more than one supplier. If this is the case the id and name are the same but with a different supplier in the arraylist.
My code so far takes care of this by creating empty cells for id and name and put the other supplier in a new cell.

But, it doesn't look good to create new rows for every supplier. What I want is that if a product has more than one supplier I want all the suppliers in the same cell on the row for the product id.

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-10-18 20:37:35

使用 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 = new List<TestProduct> 
        {
            new TestProduct
            {
                Name = "Test",
                ID = "1",
                Suppliers = new List<TestSupplier>
                {
                    new TestSupplier {  Name="Supplier1" },
                    new TestSupplier { Name = "Supplier2" },
                    new TestSupplier { Name =" A very long supplier 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; }}

示例输出:
替代文本

Instead of generating a table in code-behind use a GridView.
I've a sample here which uses GridView and a Repeater inside the GridView's Item Template.
The repeater spits out a unbounded list for each supplier.

Markup:

<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>

And code to bind the data (the type definition are below)

GridView1.DataSource = new List<TestProduct> 
        {
            new TestProduct
            {
                Name = "Test",
                ID = "1",
                Suppliers = new List<TestSupplier>
                {
                    new TestSupplier {  Name="Supplier1" },
                    new TestSupplier { Name = "Supplier2" },
                    new TestSupplier { Name =" A very long supplier name"}
                }
            }
        };

        GridView1.DataBind();

I've used sample TestProduct and 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; }}

sample output:
alt text

南巷近海 2024-10-18 20:37:35

您可以在此处使用哈希表:

        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); 
            }  

     }

You can use a Hashtable here:

        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); 
            }  

     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文