saifullah khan

saifullah khan

  • NA
  • 335
  • 300.6k

add subheading row below the gridview heading

Jul 16 2013 2:52 AM
i have a grid view i want to insert a row below the main heading i have tried alot but its not working. here is  the code behind.
protected void grMergedCellExample_RowCreated(object sender, GridViewRowEventArgs e)     {         /*          * Purpose:          *          * There are 7 cells in the grid                   * They are :          *              a)User Id          *              b)Name          *              c)Age          *              d)Address          *              e)Sex          *              f)DOB          *              g)Year          *              h)Designation          *          * Out of which, I want to merge columns c,d  (i.e.Age,Address) under the column          * name "Merged Column(Age-Address)" while columns f , g & h(i.e. DOB,Year,Designation) under the          * column name "Merged Column(DOB-Year-Designation)".          *          * Columns a & b (i.e. User Id & Name) will remain as it is so as column e i.e. Sex.                   *          */          //Check if the grid row type is header or not         if (e.Row.RowType == DataControlRowType.Header)         {             #region Object creation              //Creating a gridview object             GridView objGridView = (GridView)sender;              //Creating a gridview row object                          GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);              //Creating a table cell object             TableCell objtablecell = new TableCell();              #endregion              #region Merge cells              //Add a blank cell at the first two cell headers(i.e. User Id & Name)             //This can be achieved by making the colspan property of the table cell object as 2             // and the text property of the table cell object will be blank             //Henceforth, add the table cell object to the grid view row object             AddMergedCells(objgridviewrow, objtablecell, 1, "User ID");             AddMergedCells(objgridviewrow, objtablecell, 1, "User Name");              //Merge columns c,d (i.e.Age,Address) under the column name "Merged Column(Age-Address)"             //This can be achieved by making the colspan property of the table cell object as 2             //and setting it's text to "Merged Column(Age-Address)"             //Henceforth, add the table cell object to the grid view row object              AddMergedCells(objgridviewrow, objtablecell, 2, "Jan");               //Add a blank cell at the fifth cell header(i.e. Sex)             //This can be achieved by making the colspan property of the table cell object as 1             // and the text property of the table cell object will be blank             //Henceforth, add the table cell object to the grid view row object              AddMergedCells(objgridviewrow, objtablecell, 2, "Feb");              //Merge columns f,g,h (i.e. DOB,Year,Designation) under the column name "Merged Column(DOB-Year-Designation)"             //This can be achieved by making the colspan property of the table cell object as 3             //and setting it's text to "Merged Column(DOB-Year-Designation)"             //Henceforth, add the table cell object to the grid view row object             AddMergedCells(objgridviewrow, objtablecell, 2, "March");               //Lastly add the gridrow object to the gridview object at the 0th position             //Because,the header row position is 0.             objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);              #endregion         }     }     #region Methods      /// <summary>     /// Function : AddMergedCells     /// Purpose: Adds merged cell in the header     /// </summary>     /// <param name="objgridviewrow"></param>     /// <param name="objtablecell"></param>     /// <param name="colspan"></param>     /// <param name="celltext"></param>     private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, string celltext)     {         objtablecell = new TableCell();         objtablecell.Text = celltext;         objtablecell.ColumnSpan = colspan;         objgridviewrow.Cells.Add(objtablecell);     }     #endregion



           i have tried to change 1 to 2 but its not working.

GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

please tell me what wrong.


Answers (4)