2011-06-08 98 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是要走的路。