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

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");
        gv.Cells.Add(tbCell);

        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.

Thanks
Posted
Comments
[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.NET
<div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
           onrowdatabound="GridView1_RowDataBound">
           <Columns>
               <asp:TemplateField HeaderText="item code">
                   <ItemTemplate>
                       <asp:Label ID="lblID" runat="server" Text="<%# Bind('item_code') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Item name">
                   <ItemTemplate>
                       <asp:Label ID="lblItemName" runat="server" Text="<%# Bind('Item_name') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="brand">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text="<%# Bind('brand') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="size">
                   <ItemTemplate>
                       <asp:Label ID="lblSize" runat="server" Text="<%# Bind('size') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
   </div>

Code Behind:
C#
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();
           da.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }
   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";
           row.Cells.Add(cell);


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


          TableCell cell2 = new TableCell();
           cell2.Text = "eeeee";
           row.Cells.Add(cell2);


         TableCell  cell3 = new TableCell();
           cell3.Text = "ssss";
           row.Cells.Add(cell3);
                //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
M.Mitwalli
 
Share this answer
 
v4
Comments
Lancy.net 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 :)
Lancy.net 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