locked
column span starting from cell 4 RRS feed

  • Question

  • User219039814 posted

    I want to give headings to girdview.

    But i want to skip the first 2 columns and start my column span from the third column onwards.

    Is there a method where i can specify the start and stop range for column span

    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    TableHeaderCell cell = new TableHeaderCell();
    cell.Text = "First Year";
    cell.ColumnSpan = 2;

    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan=2
    cell.Text = "Second Year";

    row.BackColor = ColorTranslator.FromHtml("#3AC0F2");

    displayData.HeaderRow.Parent.Controls.AddAt(0, row);

    Wednesday, May 16, 2018 9:31 AM

All replies

  • User409696431 posted

    Just add empty header cells before the others, with cell.Text = "";

    (You are creating a table row.  Proper structure means that row should have the same number of cells as the other rows, as individual cells or as one of the cells combined into a span.  You want those initial empty cells to be there.)

    Wednesday, May 16, 2018 10:42 AM
  • User36583972 posted

    Hi vijaylakshmi,

    But i want to skip the first 2 columns and start my column span from the third column onwards.

    Is there a method where i can specify the start and stop range for column span

    You can use the ColumnSpan and RemoveAt to control your column span from the third column.

      <asp:GridView ID="grid"  OnRowCreated="grid_RowCreated"   runat  ="server"></asp:GridView>
    
            protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    // colspan from third column.
                    e.Row.Cells[2].ColumnSpan = 2;
                    // remove the four column
                    e.Row.Cells.RemoveAt(4);
                }
    
                //if (e.Row.RowType == DataControlRowType.DataRow)
                //{
                //}
            }
    
    

    ows and Columns Merging in ASP.NET GridView Control:

    Best Regards,

    Yong Lu

    Thursday, May 17, 2018 6:24 AM