Adding a DropDownList to DataGrid dynamically in edit mode?

G

Guest

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'
 
A

Alex Homer

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
 
G

Guest

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 said:
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 said:
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'
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top