Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 527
Arti Avatar asked Jan 25 '26 14:01

Arti


1 Answers

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.

like image 77
Arti Avatar answered Jan 28 '26 03:01

Arti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!