Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a data table with multiple rowspan columns with ListView

I need to display data from a database in a html table. I am currently using a ListView control.

I want the final HTML table to render something like the following, where some rows have a rowspan attribute greater than one. The reason for this is that some fields have several rows of information, but correspond to the same logical entry.

For example:

|---------|---------|----------|----------|
| data    | data    |data      | data     |
|         |         |----------|          |
|         |         |data      |          |
|         |         |----------|          |
|         |         |data      |          |
|---------|---------|----------|----------|
| data    | data    |data      | data     |
|         |         |----------|          |
|         |         |data      |          |
|         |         |----------|          |
|         |         |data      |          |
|---------|---------|----------|----------|
| data    | data    |data      | data     |
|         |         |----------|          |
|         |         |data      |          |
|         |         |----------|          |
|         |         |data      |          |
|         |         |----------|          |
|         |         |data      |          |
|         |         |----------|          |
|         |         |data      |          |
|---------|---------|----------|----------|

What is the easiest way to accomplish this in ASP.net?

like image 991
Flash Avatar asked Nov 20 '25 04:11

Flash


1 Answers

Not too elegant solution for ListView. The main idea is to use Repeater inside the ListView and bind all the sub-data(I mean data from the third column in your example) except the first record to it.

<asp:ListView runat="server" ID="lstData">
    <LayoutTemplate>
        <table>
            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td <%# GetRowspan((int)Eval("Data.Length")) %>>
                <%# Eval("FirstName") %>
            </td>
            <td <%# GetRowspan((int)Eval("Data.Length")) %>>
                <%# Eval("LastName") %>
            </td>
            <td>
                <%# GetFirst((IEnumerable<string>)Eval("Data")) %>
            </td>
            <td <%# GetRowspan((int)Eval("Data.Length")) %>>
                <%# Eval("Country") %>
            </td>
        </tr>
        <asp:Repeater runat="server" 
            DataSource=<%# GetRest((IEnumerable<string>)Eval("Data")) %>>
            <ItemTemplate>
                <tr>
                    <td>
                        <%# Container.DataItem %>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:ListView>

and code behind:

public override void DataBind()
{
    var item1 = new { FirstName = "John", LastName = "Doe", 
        Data = new[] { "first", "second", "third" }, Country = "US" };
    var item2 = new { FirstName = "Jane", LastName = "Doe", 
        Data = new string[] { }, Country = "CA" };
    var item3 = new { FirstName = "Joe", LastName = "Public", 
        Data = new[] { "first", "second", "third", "fourth" }, Country = "US" };

    lstData.DataSource = new[] { item1, item2, item3 };
    lstData.DataBind();
}

protected string GetRowspan(int length)
{
    if (length == 0)
        return string.Empty;
    else
        return string.Format("rowspan='{0}'", length);
}

protected string GetFirst(IEnumerable<string> data)
{
    return data.FirstOrDefault();
}

protected IEnumerable<string> GetRest(IEnumerable<string> data)
{
    if (data.Any())
        return data.Skip(1);
    else
        return Enumerable.Empty<string>();
}

this outputs data in the format you want.

But if the usage of ListView is not necessary you could take a look onto GridView. There is more elegant way to do this by using it - ASP.NET GridView RowSpan using RowCreated Event - How to add Table Dynamic RowSpan with GridView article.

like image 176
Oleks Avatar answered Nov 21 '25 18:11

Oleks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!