c# - ASP.NET - GridView 问题

标签 c# asp.net gridview

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

<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 值,但不知道如何将这 2 个单元格分组到一个 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>";

最佳答案

enter image description here

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

这是该问题的一个片段:

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,然后填充所需的单元格数据。

关于c# - ASP.NET - GridView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6280700/

相关文章:

c# - 无法在当前范围或上下文中解决。确保所有引用的变量都在范围内

asp.net - CAPI2 代理自动配置 URL 未找到

javascript - 如何使用asp.net动态添加链接按钮并在gridview中传递参数?

c# - ASP.NET HttpHandler 是否超时

c# - 获取 GridView 中选中行的索引

ASP.net gridview 图像按钮替代文本

C# 不检测用 PHP 编辑的文本文件

c# - 添加SqlDbType.Text参数时应该使用什么大小的值?

c# - ML.NET 预测纽约出租车票价 - 程序不包含适合入口点的静态 'Main' 方法

c# - 在大约 50 个 IIS Web 服务器(asp.net 和 asp 组合)上部署代码的部署技术