Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all. I would like to add a new row of data through GridView.The articles i found so far on net are using datasource controls.but my requirement is to do that without using datasource controls. So,please help with that.
Posted

Hi ,
This will give idea

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);
       }
   }


Best regards
M.Mitwalli
 
Share this answer
 
 
Share this answer
 

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