ASP.NET - GridView problem
I'm using GridView control which generates the following HTML:
<table>
<tr><td>...</td><td>...</td></tr>
<tr><td>...</td><td>...</td></tr>
开发者_StackOverflow社区 ...
</table>
I want to change the HTML of last row to something like this:
<table>
<tr><td>...</td><td>...</td></tr>
<tr><td>...</td><td>...</td></tr>
...
<tr><td colspan="2"><span>...</span><span>...</span></td></tr>
</table>
I can set the colspan value of first cell but don't know how to group those 2 cells in one TD element. Can I do that using GridView control or do I have to use Repeater?
Any help would be greatly appreciated.
EDIT
I'm using the following code to solve the problem:
// 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 regarding doing this with the Header Row
Here is a snippet from that question:
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!
}
}
}
Note my comment on t.Rows.AddAt()
... You can dynamically add a row, set the appropriate attributes, such as colspan
, and populate the cell data as desired.
If you only want this to apply to the last row why not use the footer of the gridview then? Anyway I don't see the point, purpose. Why would you want to change this, maybe you could give some more information so we know what you want to achieve.
If you only want the last row to change, then footer is the way to go.
精彩评论