D
Daniel Roth
How to add a Dropdown list to a datagrid at runtime (dynamic) without
using template columns in ASP.NET and still have the ability to us the
datagrid Update event.
1. Dynamiclly add the drowndown in Itemdatabound Event, remove the
textbox and save the UniqueID to the Viewstate
private void DatagridEvents_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
CStageCollection oStageCollection =
(CStageCollection)CStageCollection.Create(new
CStageCollection.CreateCriteria());
DataSet oDS = (DataSet)CStageCollection.Fetch(new
CStage.Csp_get_stage_name_id( Convert.ToInt32(
((TextBox)e.Item.Cells[2].Controls[0]).Text ) ));
System.Web.UI.WebControls.DropDownList dDLOriginEvent = new
System.Web.UI.WebControls.DropDownList();
foreach(DataRow dR in oDS.Tables[0].Rows)
{
dDLOriginEvent.Items.Add(dR[1].ToString());
}
dDLOriginEvent.SelectedIndex =
dDLOriginEvent.Items.IndexOf(dDLOriginEvent.Items.FindByValue(
((TextBox)e.Item.Cells[5].Controls[0]).Text) );
e.Item.Cells[5].Controls.RemoveAt(0);
e.Item.Cells[5].Controls.AddAt(0,dDLOriginEvent);
ViewState["dDLOriginEvent"] = e.Item.Cells[5].Controls[0].UniqueID;
}
}
2. In the datagrid Update event use Request.Form.Get with the
Viewstate
private void DatagridEvents_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
CEvents oEvents = new CEvents();
CStageCollection oStageCollection =
(CStageCollection)CStageCollection.Create(new
CStageCollection.CreateCriteria());
DataSet oDS = (DataSet)CStageCollection.Fetch(new
CStage.Csp_get_stage_name_id( Convert.ToInt32(
((TextBox)e.Item.Cells[2].Controls[0]).Text ) ));
foreach(DataRow dR in oDS.Tables[0].Rows)
{
if(oEvents.Origin != null)
{
break;
}
if( dR[1].ToString() ==
Request.Form.Get((string)ViewState["dDLOriginEvent"]) )
{
oEvents.Origin = dR[0].ToString();
}
}
DatagridEvents.EditItemIndex = -1;
try
{
CEvents.Save(oEvents, new CEvents.SaveCriteria());
}
catch
{
Response.Write("TicketsEvents.aspx - Please contact your System
Administrator");
}
BindDataToGridEvents();
}
Daniel Roth
MSCD.NET
using template columns in ASP.NET and still have the ability to us the
datagrid Update event.
1. Dynamiclly add the drowndown in Itemdatabound Event, remove the
textbox and save the UniqueID to the Viewstate
private void DatagridEvents_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
CStageCollection oStageCollection =
(CStageCollection)CStageCollection.Create(new
CStageCollection.CreateCriteria());
DataSet oDS = (DataSet)CStageCollection.Fetch(new
CStage.Csp_get_stage_name_id( Convert.ToInt32(
((TextBox)e.Item.Cells[2].Controls[0]).Text ) ));
System.Web.UI.WebControls.DropDownList dDLOriginEvent = new
System.Web.UI.WebControls.DropDownList();
foreach(DataRow dR in oDS.Tables[0].Rows)
{
dDLOriginEvent.Items.Add(dR[1].ToString());
}
dDLOriginEvent.SelectedIndex =
dDLOriginEvent.Items.IndexOf(dDLOriginEvent.Items.FindByValue(
((TextBox)e.Item.Cells[5].Controls[0]).Text) );
e.Item.Cells[5].Controls.RemoveAt(0);
e.Item.Cells[5].Controls.AddAt(0,dDLOriginEvent);
ViewState["dDLOriginEvent"] = e.Item.Cells[5].Controls[0].UniqueID;
}
}
2. In the datagrid Update event use Request.Form.Get with the
Viewstate
private void DatagridEvents_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
CEvents oEvents = new CEvents();
CStageCollection oStageCollection =
(CStageCollection)CStageCollection.Create(new
CStageCollection.CreateCriteria());
DataSet oDS = (DataSet)CStageCollection.Fetch(new
CStage.Csp_get_stage_name_id( Convert.ToInt32(
((TextBox)e.Item.Cells[2].Controls[0]).Text ) ));
foreach(DataRow dR in oDS.Tables[0].Rows)
{
if(oEvents.Origin != null)
{
break;
}
if( dR[1].ToString() ==
Request.Form.Get((string)ViewState["dDLOriginEvent"]) )
{
oEvents.Origin = dR[0].ToString();
}
}
DatagridEvents.EditItemIndex = -1;
try
{
CEvents.Save(oEvents, new CEvents.SaveCriteria());
}
catch
{
Response.Write("TicketsEvents.aspx - Please contact your System
Administrator");
}
BindDataToGridEvents();
}
Daniel Roth
MSCD.NET