Microsoft 以CSS 友好控制适配器的形式提供了控制适配器的参考实现
你可以看看他们是如何实现GridViewAdapter
这是处理渲染行的代码
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);
}
}