2011-06-08 56 views
2

我使用的GridView控制产生下面的HTML:ASP.NET - 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> 

我可以设置第一个单元格的值,但不知道如何将这两个单元格分组到一个TD元素中。 我可以使用GridView控制或必须使用中继器

任何帮助将不胜感激。

编辑

我用下面的代码来解决这个问题:关于与标题行

在这里做,这是一个

// 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>"; 

回答

1

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,并根据需要填充单元格数据。

1

如果你只希望这适用于最后一行为什么不使用gridview的页脚呢? 无论如何,我没有看到重点,目的。 你为什么要改变这个,也许你可以提供更多的信息,以便我们知道你想要达到什么目的。

如果你只想要最后一行改变,那么footer是要走的路。