halvorn

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?

Answers (2)