aartikaur

aartikaur

  • NA
  • 13
  • 0

Add a new row to a datagrid dynamically

Sep 30 2004 2:54 AM
hi, I have a datagrid not bound to any database table,in this datagrid i have a Dropdownlist on the Selection of any value from the DropDownlist i want to add a new row in the Datagrid with a DropDown Control. I have tried it in the following way,but somehow it doesnt work kindly let me know where I am going wrong public class SearchControl : System.Web.UI.UserControl { protected System.Web.UI.WebControls.DataGrid DataGrid1; Table Table1; private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { Table1 = new Table(); TableRow row = new TableRow( ); Table1.Rows.Add (row); DataGrid1.DataSource=Table1.Rows; DataGrid1.DataBind(); } } private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType.Equals(ListItemType.Item)) { DropDownList DropDownList1 = new DropDownList(); DropDownList1.Items.Add("And"); DropDownList1.Items.Add("Or"); DropDownList1.AutoPostBack=true; DropDownList1.SelectedIndexChanged +=new EventHandler(dd1_SelectedIndexChanged); e.Item.Cells[0].Controls.Add(DropDownList1); } } private void dd1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddl = (DropDownList) sender; DataGridItem dgi = (DataGridItem) ddl.Parent.Parent; DataGrid dg =(DataGrid) dgi.Parent.Parent; TableCell tc = new TableCell(); tc.Controls.Add(ddl); DataGridItem dgItem = new DataGridItem(dgi.ItemIndex+1,0,ListItemType.Item); dgItem.Cells.Add(tc); dg.Controls[0].Controls.AddAt(dgi.ItemIndex+1,dgItem); } } }