Click here to Skip to main content
15,914,327 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i had a grid..


outside of grid i had a textbox and add button...

now whenevr i clcik on add button the text in the textbox should add in to grid...


please help
Posted
Comments
Gitanjali Singh 7-Jan-14 22:43pm    
have you written any code for this?please post your code.

1 solution

  1. Attach a OnClick Event to the Button.
  2. Inside OnClick Event, get the TextBox Text by txtYourTextBoxID.Text.
  3. Update the DataSource of GridView by adding a new Row. If you are using DataTable, create a new DataRow for that DataTable.
  4. Assign the DataTable to GridView DataSource.
  5. Bind the GridView.
 
Share this answer
 
Comments
spanner21 7-Jan-14 23:33pm    
Hi Tadit,

i wrote code like this

DataTable dt1 = new DataTable();
dt1.Columns.Add("filename");

DataRow dtrow = dt1.NewRow();

dtrow["filename"] = uploadFile.FileName.ToString();
dt1.Rows.Add(dtrow);

grdfiledetails.DataSource = dt1;
grdfiledetails.DataBind();


but whenever i click on the attch button the old row is getting updted with the new value,,i am not seeing old row..pls sugggest
First of all, good attempt. Nice work.

Let me explain the issue in your code now.

The following line...

DataTable dt1 = new DataTable();

will always a new DataTable for you and you are assigning this to the GridView everytime Button is clicked, so the previous DataTable is lost. So, you need to update the existing DataSource.
We have to use ViewState or Session to store the existing DataTable. Take a look at the updated code.
It is tested and working fine.

protected void btnsubmit_Click(object sender, EventArgs e)
{
DataTable dt1 = null;

if (ViewState["GridDataTable"] != null)
{
dt1 = (DataTable)ViewState["GridDataTable"];
}
else
{
dt1 = new DataTable();
dt1.Columns.Add("filename");
}

DataRow dtrow = dt1.NewRow();

dtrow["filename"] = uploadFile.FileName.ToString();
dt1.Rows.Add(dtrow);

ViewState["GridDataTable"] = dt1;
grdfiledetails.DataSource = dt1;
grdfiledetails.DataBind();
}
spanner21 8-Jan-14 1:19am    
Thanks Tadit,...it worked
Glad to hear that. :) Keep coding. :)

Thanks for accepting the answer.

Regards,
Tadit

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