How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.

Discussion in 'ASP .Net Datagrid Control' started by Daniel Roth, Apr 5, 2005.

  1. Daniel Roth

    Daniel Roth Guest

    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
     
    Daniel Roth, Apr 5, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2ZXkgUA==?=

    Datagrid Template Columns At Runtime

    =?Utf-8?B?RGF2ZXkgUA==?=, Jun 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    466
    =?Utf-8?B?RGF2ZXkgUA==?=
    Jun 10, 2005
  2. =?Utf-8?B?RGF2ZSBU?=

    Columns in Dropdown list and list box

    =?Utf-8?B?RGF2ZSBU?=, Jun 25, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    510
    jufemaiz: jc
    Jun 27, 2007
  3. Kilic

    add TemplateColumn(dropdown) to DataGrid at runtime

    Kilic, Apr 27, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    179
  4. Davey P

    DataGrid Template Columns At Runtime

    Davey P, Jun 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    125
    Davey P
    Jun 10, 2005
  5. Namespace

    Dropdown list with 2 textfield columns in asp.net

    Namespace, Oct 22, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    149
    Namespace
    Oct 22, 2005
Loading...

Share This Page