【问题标题】:ASP.NET - GridView problemASP.NET - GridView 问题
【发布时间】:2011-06-08 14:48:06
【问题描述】:

我正在使用 GridView 控件,它生成以下 HTML:

<table>
    <tr><td>...</td><td>...</td></tr>
    <tr><td>...</td><td>...</td></tr>
    ...
</table>

我想把最后一行的 HTML 改成这样:

<table>
    <tr><td>...</td><td>...</td></tr>
    <tr><td>...</td><td>...</td></tr>
    ...
    <tr><td colspan="2"><span>...</span><span>...</span></td></tr>
</table>

我可以设置第一个单元格的 colspan 值,但不知道如何将这两个单元格分组到一个 TD 元素中。 我可以使用 GridView 控件来做到这一点,还是必须使用 Repeater?

任何帮助将不胜感激。

编辑

我正在使用以下代码来解决问题:

// get cell values
string firstValue = lastRow.Cells[0].Text;
string secondValue = lastRow.Cells[1].Text;

// remove the second cell
lastRow.Cells.RemoveAt(1);
// set column span
lastRow.Cells[0].ColumnSpan = 2;
// set text inside TD element
lastRow.Cells[0].Text = "<span>" + totalText + @"</span><span>" +
                        totalConsumptionText + @"</span>";

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    Nice Discussion here 关于使用标题行进行此操作

    这是该问题的一个sn-p:

    protected void gvOrganisms_DataBound(object sender, EventArgs e)
    {
        GridView grid = sender as GridView;
    
        if (grid != null)
        {
            GridViewRow row = new GridViewRow(0, -1,
                DataControlRowType.Header, DataControlRowState.Normal);
    
            TableCell left = new TableHeaderCell();
            left.ColumnSpan = 3;
            row.Cells.Add(left);
    
            TableCell totals = new TableHeaderCell();
            totals.ColumnSpan = grid.Columns.Count - 3;
            totals.Text = "Totals";
            row.Cells.Add(totals);
    
            Table t = grid.Controls[0] as Table;
            if (t != null)
            {
                t.Rows.AddAt(0, row); // You will change this line to insert at the end!
            }
        }
    }
    

    注意我对t.Rows.AddAt()的评论...您可以动态添加一行,设置适当的属性,例如colspan,并根据需要填充单元格数据。

    【讨论】:

      【解决方案2】:

      如果您只想将此应用于最后一行,那么为什么不使用 gridview 的页脚呢? 无论如何,我没有看到重点,目的。 您为什么要更改此设置,也许您可​​以提供更多信息,以便我们知道您想要实现的目标。

      如果您只想更改最后一行,那么页脚就是要走的路。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        • 2011-10-30
        相关资源
        最近更新 更多