how to add a row at a runtime by clicking a linkbutton in asp.net using c#
how to add a row at a runtime by clicking a linkbutton in asp.net using c#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Apr 10, 2012, 10:05 AM
what is your requirement here?
do you have several textbox outside the grid and after filling all the value click on control should add into grid?
or
you have a link called "Add row" outside the grid and click on that should enable the textbox in the every column of grid?
please clarify your requirement.
Satyapriya NayakPosted Apr 10, 2012, 9:45 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication13._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication13
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
static DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable("student");
dt.Columns.Add("sid", typeof(string));
dt.Columns.Add("sname", typeof(string));
dt.Columns.Add("smarks", typeof(string));
dt.Columns.Add("saddress", typeof(string));
}
}
public void BindData()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void lnkBtnAddToTable_Click(object sender, EventArgs e)
{
dt.Rows.Add(txtid.Text, txtname.Text, txtmarks.Text, txtaddress.Text);
BindData();
}
protected void BtnSendToDatabase_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into student(sid,sname,smarks,saddress) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
Response.Write("Records inserted successfully");
}
}
}
Thanks
Veera ChennaPosted Apr 10, 2012, 9:15 AM
take a look at the below link for step-by-step tutorial on how to add a new row at runtime to the gridview
http://technico.qnownow.com/2012/03/29/how-to-insert-update-delete-rows-in-asp-net-gridview-control/
Jignesh TrivediPosted Apr 10, 2012, 9:11 AM
hi,
Are you talking about adding row in datagrid or grid view?
If yes then add row in data souce.
for example if you use datatable to bind grid then on addition row add in that data souce.
are you getting my point?
Let me know if you have any query.
hope this help.