Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 10, 2012, 5:11 AM EDT
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.
Veera Chenna26 reputationPosted Apr 10, 2012, 5:15 AM EDT
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/
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 10, 2012, 5:45 AM EDT
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
SenthilkumarRank #387 · 6.5K reputationPosted Apr 10, 2012, 6:05 AM EDT
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.