Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello
Please help me to solve this problem, i want to add a row at runtime to gridview when user clicks on a button. i have 2 rows, i want to add a new row in grid view when user clicks "Add Button", i dont want to add data in the new row
Posted
Updated 25-Feb-12 2:29am
v2
Comments
dimpledevani 25-Feb-12 8:17am    
have you assigned a datasource to the gridview???
AtifImtiaz 25-Feb-12 8:45am    
yes gridview is populating from database, but i want to create 1 more row at runtime, obviously when user clicks on a certain button

First Check Rowcount of grid
C#
CurrentRow = dg.Rows.Count - 1;

then add data to grid
C#
dg.Rows[CurrentRow].Cells["YourFieldName1"].Value = txtText1.Text;
dg.Rows[CurrentRow].Cells["YourFieldName2"].Value = txtText2.Text;
 
Share this answer
 
Comments
PRAKASH9 25-Feb-12 8:47am    
what is the datatype of currentrow ???????????
plz make it sure
is it int or ......... ?
AtifImtiaz 25-Feb-12 8:59am    
all rows have almost same code

<asp:TemplateField HeaderText="Add" ItemStyle-VerticalAlign="Middle" ItemStyle-Width="30%" HeaderStyle-HorizontalAlign="Left">
<itemtemplate>
<asp:Button ID="btnAdd" CommandName="Add" Text="+" runat="server" onclick="btnAdd_Click"/>



this is the template for button
PRAKASH9 25-Feb-12 8:58am    
This solution is totally wrong

If you think it is a waste of time then don't give ans ............
AtifImtiaz 25-Feb-12 9:00am    
using item template
first add "text box","datagrid" and "button"

C#
in button click(write this code)
{

DataTable dt = new DataTable();
            dt = new DataTable();
            dt.Columns.Add("Val1");
           

            DataRow dr = dt.NewRow();
            dr["Val1"] = TextBox1.Text;
          
            dt.Rows.Add(dr);

            GridView1.DataSource = dt;
            GridView1.DataBind(); 
}
 
Share this answer
 
v2

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