Skip to main content

Delete a row in a detailtable object

The following code can be used to delete rows in a DetailTable object.

In the example, there is a CheckBox added to the DetailTable. This object is used for the selection of rows that are requested to be deleted. Data that is added to the DetailsGrid1 object that does not have Check1 is transferred to a DataTable. All rows on DetailsGrid1 are then deleted and repopulated with the data in this DataTable.

using System.Web.UI.WebControls; using System.Data;

DataTable dt= new DataTable();

Dt. columns.add("text1");

Dt. columns.add("text2");

for(int i=0; i<DetailsGrid1.CurrentRowCount; i++)

{

if(!( (CheckBox) DetailsGrid1.GetRowObject(i,"Selection1")). Checked)

{

DataRow dr = dt. NewRow();

dr["Text1"] = ((TextBox)DetailsGrid1.GetRowObject(i,"Text1")). Text;

dr["Text2"] = ((TextBox)DetailsGrid1.GetRowObject(i,"Text2")). Text;

Dt. Rows.Add(dr);

}

}

DetailsGrid1.CurrentRowCount=0;

for(int y=0; y<dt. Rows.Count; y++)

{

DetailsGrid1.CurrentRowCount++;

((TextBox)DetailsGrid1.GetRowObject(y,"Text1")). Text=dt. rows[y]["text1"]. ToString();

((TextBox)DetailsGrid1.GetRowObject(y,"Text2")). Text=dt. rows[y]["text2"]. ToString();

((CheckBox)DetailsGrid1.GetRowObject(y,"Selection1")). Checked=false;

}