exploit344
asked on
adding row to gridview on buton click
i have three textbox and a button...i want to add row to gridview on button click...and put values of textbox n gridview textbox....
eg.. i have 3 texboxs t1,t2,t3 and button b
on button click b...
add text of t1 to gridview cell[0] ,t2 text to gridview cell[1]. t3 text to gridview[2]...
and one thing more i don't wanna replace any row..
on one button click 1st row will be added
on 2nd button click 2nd row will be added.. so .on..
then i am gonna save all values to database...
eg.. i have 3 texboxs t1,t2,t3 and button b
on button click b...
add text of t1 to gridview cell[0] ,t2 text to gridview cell[1]. t3 text to gridview[2]...
and one thing more i don't wanna replace any row..
on one button click 1st row will be added
on 2nd button click 2nd row will be added.. so .on..
then i am gonna save all values to database...
ASKER
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
}
else
{
dt = (DataTable)ViewState["Data Table"];
}
ViewState["DataTable"] = dt;
}
private void MakeDataTable()
{
dt.Columns.Add("Name");
dt.Columns.Add("Number");
}
protected void Button2_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr["Name"] = txtName.Text;
dr["Number"] = txtNumber.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
this code is working fine.. but this is for simple gridview..
i mean i have textbox in gridview.. now how do i put values of textbox above gridview in to textbox...
cause at last i have to save gridview into table.. so my prob is i have textbox in gridview.. so can u please modify this code according to textbox inside gridview.. i mean insert values to gridview textbox
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
}
else
{
dt = (DataTable)ViewState["Data
}
ViewState["DataTable"] = dt;
}
private void MakeDataTable()
{
dt.Columns.Add("Name");
dt.Columns.Add("Number");
}
protected void Button2_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr["Name"] = txtName.Text;
dr["Number"] = txtNumber.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
this code is working fine.. but this is for simple gridview..
i mean i have textbox in gridview.. now how do i put values of textbox above gridview in to textbox...
cause at last i have to save gridview into table.. so my prob is i have textbox in gridview.. so can u please modify this code according to textbox inside gridview.. i mean insert values to gridview textbox
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thnx
http://forums.asp.net/t/1147158.aspx