Arti
Arti

Reputation: 3071

Colspan=2 in a gridview

How to have colspan=2 in gridview? I want the gridview to have 2 columns under 1 header. How can I achieve this? I tried following code but it doesn't seem to work. Any ideas??

     <asp:TemplateField>
        <HeaderStyle Width="40px" BorderWidth="1px" BorderColor="#DDDDDD" BorderStyle="Solid" />
        <ItemStyle CssClass="img_center" />
        <HeaderTemplate><b>Action</b></HeaderTemplate>
        <ItemTemplate>
           <a href="#"><img src="images/edit.png" height="20" width="20" alt="Edit" title="Edit"></a>
        </ItemTemplate>
        <ItemTemplate>
           <a href="#"><img src="images/view.png" height="20" width="20" alt="view" title="view">
        </ItemTemplate>
     </asp:TemplateField>

This is how gridview should look: enter image description here

The image shown is a table with 3 columns Sr., Action and Dept name. Action column has colspan=2, hence there are 2 columns under Action column.

Upvotes: 3

Views: 11306

Answers (1)

Arti
Arti

Reputation: 3071

Here is how I finally achieved it:

protected void gridList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[1].Attributes.Add("colspan", "2");
        }
}

First I added 4 colums to the gridview (Sr., ActionEdit, ActionView, DepartmentName) and then merged cell 2 and 3 to get the desired result.

Upvotes: 3

Related Questions