Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I have bind the data from database and show on girdview using this code.
Now I want to add a button on gridview for adding new row on gridview.
Please help me to add new row on gridview.
if (!Page.IsPostBack)
{
    binddata();
}
 
}
private void binddata()
{
    string connstring = @"Data Source=LENOVO-41B3F2CE\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;Pooling=False";
    string query = "SELECT * FROM ['ISB VAS Nodes$']";
    SqlDataAdapter da = new SqlDataAdapter(query, connstring);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
Posted 17-Apr-13 22:48pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi,
 
You can do this in two ways:
 
1. Either you insert the data in database then bind the new data to Gridview
 
2. Or else take Gridview data in a datatable and insert data into datatable and then bind new
data from the datatable to Gridview.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
copy the datatable to the session, and on click of the button add a row to datatable and bind the gridview with the new datatabl;e and copy the datatable back to session.
private void binddata()
{
    string connstring = @"Data Source=LENOVO-41B3F2CE\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;Pooling=False";
    string query = "SELECT * FROM ['ISB VAS Nodes$']";
    SqlDataAdapter da = new SqlDataAdapter(query, connstring);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    Session["data"] = dt; //copy the datatable to session.
    GridView1.DataBind();
}
Button click event :
DataTable dt = ((DataTable)Sessio["data"]);
if(dt != null)
{
    dt.Rows.Add("c1","c2","c3"); // add your the column values
    dt.AcceptChanges();
    GridView1.DataSource = dt;
    Session["data"] = dt; //copy the datatable back to session.
    GridView1.DataBind();
}
 
further refer below articles.
Insert a new row to gridview[^]
Dynamically adding and deleting rows from ASP.NET GridView[^]
 
hope it helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi..
 
Write code in your addnewrow button onclick,
just add blow code.
 

GridView1.ShowFooter=True;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

private void BindGrid(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String)));

if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();

}
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dt.Rows.Add(dr);

}
else
{
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dt.Rows.Add(dr);

}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100