473,508 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a DropDownList to DataGrid dynamically in edit mode?

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'
Nov 19 '05 #1
2 3727
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" <Da**@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
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'

Nov 19 '05 #2
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" <Da**@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
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'


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3614
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
2
2224
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. ...
3
4856
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
2954
by: NewToDotNet | last post by:
Hi, I am very new to ASP.NET and web programming in general. I have one issue. I have a Datagrid object with Edit template. In one Datagrid row, I have 1 DropdownList, 1 textbox and 1 readonly...
1
2649
by: George Durzi | last post by:
When my datagrid is in edit mode, one of my columns is edited using a drop down list. I'm able to bind the DropDownList to a DataSource when in edit mode. HOWEVER, I can't preset the...
3
4360
by: Tim::.. | last post by:
Can someone tell me how I get the correct selected office in my drop down list when I enter edit mode. At the moment it just defaults to the office at the top of the drop down list rather than...
3
4363
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
0
1190
by: bh | last post by:
I'm trying to edit data in a datagrid through a dropdown list with different id/text values. The problem comes in when I click the edit button & nothing appears to be happening. Did I do...
1
8835
by: pleaseexplaintome | last post by:
Hi all, I have a datagrid with a dropdownlist and would like to have the dropdownlist display a database value correctly while the grid is in edit mode. I have a templatecolumn as follows: ...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.