I would like to make a simple adapter for the Gridview control that would render the cell contents inside a <div>
tag.
Instead of rendering as
<table>
<tr>
<td>Some Data</td>
<td>Some Data</td>
</tr>
</table>
I would like it to render as
<table>
<tr>
<td><div>Some Data</div></td>
<td><div>Some Data</div></td>
</tr>
</table>
I understand this can be done other ways using Jquery or in RowDataBound, but I specifically would like to do it with an adapter.
I seems to me the change would be simple if there was a way to see the code for an adapter that creates the default .Net Gridview, however I do not know how to obtain that code.
Any help is greatly appreciated.
Microsoft provides a reference implementation of Control adapters in form of the CSS Friendly Control Adapters
You can take a look at how they have implemented the GridViewAdapter
Here is the bit of the code that deals with rendering the rows
private void WriteRows(HtmlTextWriter writer, GridView gridView, GridViewRowCollection rows, string tableSection)
{
if (rows.Count > 0)
{
writer.WriteLine();
writer.WriteBeginTag(tableSection);
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
foreach (GridViewRow row in rows)
{
if (!row.Visible)
continue;
writer.WriteLine();
writer.WriteBeginTag("tr");
string className = GetRowClass(gridView, row);
if (!String.IsNullOrEmpty(className))
{
writer.WriteAttribute("class", className);
}
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
foreach (TableCell cell in row.Cells)
{
DataControlFieldCell fieldCell = cell as DataControlFieldCell;
if ((fieldCell != null) && (fieldCell.ContainingField != null))
{
DataControlField field = fieldCell.ContainingField;
if (!field.Visible)
{
cell.Visible = false;
}
// Apply item style CSS class
TableItemStyle itemStyle;
switch (row.RowType)
{
case DataControlRowType.Header:
itemStyle = field.HeaderStyle;
// Add CSS classes for sorting
SetHeaderCellSortingClass(gridView, field, itemStyle);
break;
case DataControlRowType.Footer:
itemStyle = field.FooterStyle;
break;
default:
itemStyle = field.ItemStyle;
break;
}
if (itemStyle != null && !String.IsNullOrEmpty(itemStyle.CssClass))
{
if (!String.IsNullOrEmpty(cell.CssClass))
cell.CssClass += " ";
cell.CssClass += itemStyle.CssClass;
}
}
writer.WriteLine();
cell.RenderControl(writer);
}
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("tr");
}
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag(tableSection);
}
}