I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not

  • Thread starter Praveen Balanagendra via .NET 247
  • Start date
P

Praveen Balanagendra via .NET 247

here is the source code

private void AddRow()
{
TableCell tc = new TableCell();
tc.Controls.Add(new LiteralControl("NewRow"));

DataGridItem di = new DataGridItem(DataGrid1.Items.Count+1,DataGrid1.Items.Count+1,ListItemType.Item);
di.Cells.Add(tc);
di.Cells.Add(tc1);
Table t = (Table)DataGrid1.Controls[0];
t.Rows.Add(di);
}

before i call this function, say i have 5 rows in my datagrid. After this function is called, which adds a new row to datagrid, still my datagrid shows it has only 5 rows....what could be the reason
 
J

John Saunders

Praveen Balanagendra via .NET 247 said:
here is the source code

private void AddRow()
{
TableCell tc = new TableCell();
tc.Controls.Add(new LiteralControl("NewRow"));

DataGridItem di = new DataGridItem(DataGrid1.Items.Count+1,DataGrid1.Items.Count+1,ListItemType.It
em);
di.Cells.Add(tc);
di.Cells.Add(tc1);
Table t = (Table)DataGrid1.Controls[0];
t.Rows.Add(di);
}

This code isn't adding rows to a datagrid, it's adding rows to a table which
happens to be in the datagrid.

Have you set the DataSource property of the datagrid? If so, then all the
rows will come from the datasource, and the way to add a row to the grid
would be to add the row to the source.
 
J

Jos

Praveen Balanagendra via .NET 247 said:
here is the source code

private void AddRow()
{
TableCell tc = new TableCell();
tc.Controls.Add(new LiteralControl("NewRow"));

DataGridItem di = new DataGridItem(DataGrid1.Items.Count+1,DataGrid1.Items.Count+1,ListItemType.It
em);
di.Cells.Add(tc);
di.Cells.Add(tc1);
Table t = (Table)DataGrid1.Controls[0];
t.Rows.Add(di);
}

before i call this function, say i have 5 rows in my datagrid. After this
function is called, which adds a new row to datagrid, still my datagrid
shows it has only 5 rows....what could be the reason


1) AddRow() is never called
2) DataBind() is being called after AddRow()
3) I'm not sure this could be the reason, but try:
new DataGridItem(DataGrid1.Items.Count+1,0,ListItemType.Item);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top