Click here to Skip to main content
15,939,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Below is code to add a empty row in Gridview

protected void PrintGv_PreRender(object sender, EventArgs e)
       GridViewRow gv = new GridViewRow(1,-1,DataControlRowType.Header,DataControlRowState.Normal);
        TableCell tbCell = new TableCell();
        tbCell.ColumnSpan = 3;
        tbCell.Text = "GridView Header";
        tbCell.Attributes.Add("style", "text-align:center");

        PrintGv.Controls[0].Controls.AddAt(1, gv);


But i am getting this error
Specified argument was out of the range of valid values.
Parameter name: index

Need your help to solve this issue.

[no name] 8-Apr-12 14:20pm    
Is PrintGv.Controls[0] a proper value?

1 solution

Hi ,
See This Example it will Give you Idea :),
Hope it's help you .
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
               <asp:TemplateField HeaderText="item code">
                       <asp:Label ID="lblID" runat="server" Text="<%# Bind('item_code') %>"></asp:Label>
               <asp:TemplateField HeaderText="Item name">
                       <asp:Label ID="lblItemName" runat="server" Text="<%# Bind('Item_name') %>"></asp:Label>
               <asp:TemplateField HeaderText="brand">
                       <asp:Label ID="Label2" runat="server" Text="<%# Bind('brand') %>"></asp:Label>
               <asp:TemplateField HeaderText="size">
                       <asp:Label ID="lblSize" runat="server" Text="<%# Bind('size') %>"></asp:Label>

Code Behind:
protected void Page_Load(object sender, EventArgs e)
       if (!IsPostBack)
           var con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
           string statment = "select item_code, Item_name, brand, size  from Items ";
           SqlDataAdapter da = new SqlDataAdapter(statment, con);
           DataTable dt = new DataTable();
           GridView1.DataSource = dt;
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       //Check if it's header
       if (e.Row.RowType == DataControlRowType.Header)
                 //Create new Row in Gridview
         GridViewRow  row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
             //create Cell and it will be added to row
          TableCell   cell = new TableCell();
           cell.Text = "0";

           TableCell cell1 = new TableCell();
           cell1.Text = "Item Name Test";

          TableCell cell2 = new TableCell();
           cell2.Text = "eeeee";

         TableCell  cell3 = new TableCell();
           cell3.Text = "ssss";
                //add the row to Gridview and AddAT(Index of where it will be appear
            GridView1.Controls[0].Controls.AddAt(1, row);

If you still have problem let me know .
Best Regards
Share this answer
Comments 8-Apr-12 23:44pm    
Hi Thanks for your reply....
I need to create a row after the header template could u pls assist?
Mohamed Mitwalli 9-Apr-12 1:15am    
Hi your welcome :) just see the example in the link if didn't help you let me know and i will make sample for you :) 9-Apr-12 6:07am    
Hi i tried with the example i can add a row after item template but not after header...Your help needed.
Mohamed Mitwalli 10-Apr-12 2:00am    
sorry for being late i was busy little i did the Sample for you take a look and if still something not clear let me know
Mohamed Mitwalli 10-Apr-12 13:45pm    
check the new solution and let me know .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900