0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Jeremy A. West
  • 2,162
  • 4
  • 27
  • 40

1 Answers1

3

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);
    }
}
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • I had a strong feeling someone was going to point me in this direction. Unfortunately for me the CSSFriendly Adapters make additional changes to rendering that for this project will cause me additional work. However, I have a feeling this may be the only option. I wish I could see the render methods for the way .NET renders grid views without applying adapters. – Jeremy A. West Sep 13 '12 at 19:51
  • That being said, if no one else can give me exactly what I am looking for I will accept this answer (I'm going let it sit for the weekend) +1 – Jeremy A. West Sep 13 '12 at 19:53
  • I am not suggesting you use the CSS friendly adapters. I am saying that you write your own. Then you can output exactly what output you want – nunespascal Sep 14 '12 at 03:24