Adding a DropDownList to DataGrid dynamically in edit mode?

Discussion in 'ASP .Net' started by =?Utf-8?B?RGF2ZQ==?=, Dec 16, 2004.

  1. Hi,

    I'm building a maintenance form for a table and some of the fields are
    textboxes (i.e name) and some should be dropdowns (i.e country of origin)

    When a user clicks 'Edit' in the <asp:EditCommandColumn> I want either a
    textbox or dropdown to appear when I check what is being edited.

    I thought I could create the dropdown on the fly and add it to the datagrid
    as below but It's not appearing. I imagined I could populate the dropdown
    with codes at this point too.

    private void dgCompany_Edit(Object sender, DataGridCommandEventArgs e)
    {
    dgCompany.EditItemIndex = e.Item.ItemIndex;

    DropDownList ddwn = new DropDownList();
    ddwn.ID = "dynamicDdwn";
    e.Item.Cells[0].Controls.Add(ddwn);

    }

    Is this approach valid? I thought of maybe creating BOTH the textbox and the
    dropdown at design time and hide or show the control in the Edit event using

    ((TextBox) e.Item.FindControl("txtValue")).Visible = false;
    or
    ((DropDownList) e.Item.FindControl("listValue")).Visible = false;

    but I got 'object not set to instance of object'
    =?Utf-8?B?RGF2ZQ==?=, Dec 16, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?RGF2ZQ==?=

    Alex Homer Guest

    How about declaring it in the <EditItemTemplate> section, and then
    populating it during the OnItemDatabound event? You need to check the the
    ListItemType to make sure you only do this for rows that are in edit mode.

    Sub MyItemDataboundhandler(objSender As Object, objArgs As
    DataListItemEventArgs)
    'sets value of DropDownList in each row to current Shipper name

    'see what type of row caused the event
    Dim objItemType As ListItemType = CType(objArgs.Item.ItemType,
    ListItemType)

    'only set the row variations for an EditItem row, which occurs only
    'once for each page load, and contains the EditItemTemplate content
    If objItemType = ListItemType.EditItem Then

    'get a reference to the asp:DropDownList control in this row
    Dim objList As DropDownList =
    CType(objArgs.Item.FindControl("lstShipper"), DropDownList)

    'get Shippers DataSet using function elsewhere in this page
    Dim objShipDataSet As DataSet = GetShippersFromSessionOrServer()

    objList.DataSource = objShipDataSet
    objList.DataTextField = "ShipperName"
    objList.DataValueField = "ShipperID"
    objList.DataBind()

    'objArgs.Item.DataItem returns the data for this row of items
    Dim objRowVals As DataRowView = CType(objArgs.Item.DataItem,
    DataRowView)

    'get the Shipping Company ID of the item in the DataRowView
    Dim intShipperID As Integer = objRowVals("ShipVia")

    Dim objItem As ListItem
    For Each objItem In objList.Items
    If objItem.Value = intShipperID Then
    objItem.Selected = True
    End If
    Next

    End If


    "Dave" <> wrote in message
    news:...
    > Hi,
    >
    > I'm building a maintenance form for a table and some of the fields are
    > textboxes (i.e name) and some should be dropdowns (i.e country of origin)
    >
    > When a user clicks 'Edit' in the <asp:EditCommandColumn> I want either a
    > textbox or dropdown to appear when I check what is being edited.
    >
    > I thought I could create the dropdown on the fly and add it to the

    datagrid
    > as below but It's not appearing. I imagined I could populate the dropdown
    > with codes at this point too.
    >
    > private void dgCompany_Edit(Object sender, DataGridCommandEventArgs e)
    > {
    > dgCompany.EditItemIndex = e.Item.ItemIndex;
    >
    > DropDownList ddwn = new DropDownList();
    > ddwn.ID = "dynamicDdwn";
    > e.Item.Cells[0].Controls.Add(ddwn);
    >
    > }
    >
    > Is this approach valid? I thought of maybe creating BOTH the textbox and

    the
    > dropdown at design time and hide or show the control in the Edit event

    using
    >
    > ((TextBox) e.Item.FindControl("txtValue")).Visible = false;
    > or
    > ((DropDownList) e.Item.FindControl("listValue")).Visible = false;
    >
    > but I got 'object not set to instance of object'
    Alex Homer, Dec 17, 2004
    #2
    1. Advertising

  3. Thanks! I'll give that a try. Just for kicks I got the dynamic dropdown to
    work (since I like to explore all options)

    dgCompanyCodes.EditItemIndex = e.Item.ItemIndex;
    BindDataGrid();

    DropDownList ddwn = new DropDownList();
    ddwn.ID = "dynamicDdwn";
    ddwn.Items.Add("a");
    ddwn.Items.Add("b");
    dgCompanyCodes.Items[e.Item.ItemIndex].Cells[3].Controls.Add(ddwn);

    BUT, I can't seem to access the value in the Update event so can grab what
    the user entered!

    Is the dyanamically created control available on postback? I've heard you
    have to re-create dynamic controls each time the page is rendered but I
    thought I would be able to access it *during* the postback (or
    datagrid_update in this case).

    Thanks again for any ideas. Dave.

    "Alex Homer" wrote:

    > How about declaring it in the <EditItemTemplate> section, and then
    > populating it during the OnItemDatabound event? You need to check the the
    > ListItemType to make sure you only do this for rows that are in edit mode.
    >
    > Sub MyItemDataboundhandler(objSender As Object, objArgs As
    > DataListItemEventArgs)
    > 'sets value of DropDownList in each row to current Shipper name
    >
    > 'see what type of row caused the event
    > Dim objItemType As ListItemType = CType(objArgs.Item.ItemType,
    > ListItemType)
    >
    > 'only set the row variations for an EditItem row, which occurs only
    > 'once for each page load, and contains the EditItemTemplate content
    > If objItemType = ListItemType.EditItem Then
    >
    > 'get a reference to the asp:DropDownList control in this row
    > Dim objList As DropDownList =
    > CType(objArgs.Item.FindControl("lstShipper"), DropDownList)
    >
    > 'get Shippers DataSet using function elsewhere in this page
    > Dim objShipDataSet As DataSet = GetShippersFromSessionOrServer()
    >
    > objList.DataSource = objShipDataSet
    > objList.DataTextField = "ShipperName"
    > objList.DataValueField = "ShipperID"
    > objList.DataBind()
    >
    > 'objArgs.Item.DataItem returns the data for this row of items
    > Dim objRowVals As DataRowView = CType(objArgs.Item.DataItem,
    > DataRowView)
    >
    > 'get the Shipping Company ID of the item in the DataRowView
    > Dim intShipperID As Integer = objRowVals("ShipVia")
    >
    > Dim objItem As ListItem
    > For Each objItem In objList.Items
    > If objItem.Value = intShipperID Then
    > objItem.Selected = True
    > End If
    > Next
    >
    > End If
    >
    >
    > "Dave" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I'm building a maintenance form for a table and some of the fields are
    > > textboxes (i.e name) and some should be dropdowns (i.e country of origin)
    > >
    > > When a user clicks 'Edit' in the <asp:EditCommandColumn> I want either a
    > > textbox or dropdown to appear when I check what is being edited.
    > >
    > > I thought I could create the dropdown on the fly and add it to the

    > datagrid
    > > as below but It's not appearing. I imagined I could populate the dropdown
    > > with codes at this point too.
    > >
    > > private void dgCompany_Edit(Object sender, DataGridCommandEventArgs e)
    > > {
    > > dgCompany.EditItemIndex = e.Item.ItemIndex;
    > >
    > > DropDownList ddwn = new DropDownList();
    > > ddwn.ID = "dynamicDdwn";
    > > e.Item.Cells[0].Controls.Add(ddwn);
    > >
    > > }
    > >
    > > Is this approach valid? I thought of maybe creating BOTH the textbox and

    > the
    > > dropdown at design time and hide or show the control in the Edit event

    > using
    > >
    > > ((TextBox) e.Item.FindControl("txtValue")).Visible = false;
    > > or
    > > ((DropDownList) e.Item.FindControl("listValue")).Visible = false;
    > >
    > > but I got 'object not set to instance of object'

    >
    >
    >
    =?Utf-8?B?RGF2ZQ==?=, Dec 17, 2004
    #3
    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?TmV3VG9Eb3ROZXQ=?=

    TextBox and DropDownList in Datagrid Edit mode

    =?Utf-8?B?TmV3VG9Eb3ROZXQ=?=, Jan 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    790
    =?Utf-8?B?TmV3VG9Eb3ROZXQ=?=
    Feb 9, 2004
  2. Luis Esteban Valencia

    dropdownlist in datagrid edit mode

    Luis Esteban Valencia, Sep 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    Luis Esteban Valencia
    Sep 29, 2004
  3. =?Utf-8?B?VGltOjouLg==?=

    Datagrid DropdownList in edit mode???

    =?Utf-8?B?VGltOjouLg==?=, Dec 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,313
    Saravana
    Dec 22, 2004
  4. Frank Kurka
    Replies:
    8
    Views:
    304
  5. jobs at webdos
    Replies:
    0
    Views:
    464
    jobs at webdos
    Oct 9, 2006
Loading...

Share This Page