我正在使用生成以下 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>";
最佳答案
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/