views:

30

answers:

1

How do I add a colgroup tag to the datagrid control so that I can style each column using css?

+2  A: 

I thought this was addressed in .NET 3.5, but I can't find any references. Anyways, here is a hand-rolled server control that allows you to specify colgroup...

public class ColGroupGridView : GridView
{
    private ColGroup _ColGroup = null;
    private ITemplate _ColGroupTemplate = null;

    [TemplateContainer(typeof(ColGroup))]
    public virtual ITemplate ColGroupTemplate
    {
        get { return _ColGroupTemplate; }
        set { _ColGroupTemplate = value; }
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        _ColGroup = new ColGroup();
        ColGroupTemplate.InstantiateIn(_ColGroup);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        // Get the base class's output
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);
        string output = sw.ToString();
        htw.Close();
        sw.Close();

        // Insert a <COLGROUP> element into the output
        int pos = output.IndexOf("<tr");

        if (pos != -1 && _ColGroup != null)
        {
            sw = new StringWriter();
            htw = new HtmlTextWriter(sw);
            _ColGroup.RenderPrivate(htw);
            output = output.Insert(pos, sw.ToString());
            htw.Close();
            sw.Close();
        }

        // Output the modified markup
        writer.Write(output);
    }
}

internal class ColGroup : WebControl, INamingContainer
{
    internal void RenderPrivate(HtmlTextWriter writer)
    {
        writer.Write("<colgroup>");
        base.RenderContents(writer);
        writer.Write("</colgroup>");
    }
}

Use it like this...

<custom:ColGroupGridView ... runat="server">
    <ColGroupTemplate>
        <col class="itemid" />
        <col class="cover-image" />
        <col class="title" />
        <col class="number" />
        <col class="year" />
        <col class="rating" />
        <col class="cgc-rating" />
        <col class="description" />
    </ColGroupTemplate>
    <!-- Rest of stuff here... -->
</custom:ColGroupGridView>

Source: Jeff Prosise's Blog

Josh Stodola
How would I add a col programatically?For example I'd like to do something likeColGroupGridView.ColGroup.Add(col) or something similar
Arizona1911
Sure. Make a public get/set for the _ColGroup variable. Then you should be able to add like this... `Dim Col As New HtmlGenericControl("COL") Col.Attributes.Add("class", "myCssClass") ColGroupGridView.ColGroup.Add(Col)`
Josh Stodola
Can you explain what ColGroupTemplate is and how I am supposed to initialize it?So far I have thispublic void AddCol(string colClass){HtmlGenericControl col = new HtmlGenericControl("COL");col.Attributes.Add("class", colClass);_ColGroup = new ColGroup();_ColGroup.Controls.Add(col);ColGroupTemplate.InstantiateIn(_ColGroup);}The last line gives me a Object reference not set to an instance of an objectbecause ColGroupTemplate is null.
Arizona1911
@Arizona1911 Dont worry about the template, that allows you to control it via markup. You need to expose the _ColGroup variable to be public. That is essentially the `<colgroup>` tag, and it inherits Control which allows you to embed other controls within it. You can use the aforementioned snippet to add `<col>` to the group.
Josh Stodola