TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
halvorn
NA
33
0
Adding row to a datagrid
Aug 15 2006 1:00 PM
Hello, I am coding i .net using c# as codebehind. I have a datagrid that I want to add one or more new rows to, and the new rows should be added at the bottom of the grid. The datagrid is not bound to any database.
The datagrid is named DataGrid1. Is there any way of just adding a new row to this datagrid whitout clearing all information that is stored in the datagrid from before?
I`m doing something like this now:
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Text", typeof(string)));
dt.Columns.Add(new DataColumn("LineID", typeof(Int32)));
dt.Columns.Add(new DataColumn("repeats", typeof(string)));
foreach (DataGridItem dataGridItem in Datagrid1.Items)
{
repeats = ((TextBox)dataGridItem.FindControl("repeats")).Text;
if (repeats != "")
{
for (int i = 0; i < Convert.ToInt32(repeats); i++)
{
dr = dt.NewRow();//Adds a new row and adds text and id to this row
dr["LineID"] = lineId;
dr["Text"] = textline;
dt.Rows.Add(dr);
}
}
}
Datagrid1.DataSource = dt;
Datagrid1.DataBind();
Here I am searching for a value in the grid, if I find that value a new row should be added. But this removes all rows in the datagrid except the only one that is added here.
Is there any easy way of just adding a row to a datagrid that already is printed? There is no database connection involved, the information is not supposed to be stored.
Anybody knows how to do this?
Reply
Answers (
2
)
Accessing page/master page methods from enclosed controls
making a column invisible in a datagrid