Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding a DropDownList to DataGrid dynamically in edit mode?

Reply
Thread Tools

Adding a DropDownList to DataGrid dynamically in edit mode?

 
 
=?Utf-8?B?RGF2ZQ==?=
Guest
Posts: n/a
 
      12-16-2004
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'
 
Reply With Quote
 
 
 
 
Alex Homer
Guest
Posts: n/a
 
      12-17-2004
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 aspropDownList 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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'



 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGF2ZQ==?=
Guest
Posts: n/a
 
      12-17-2004
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 aspropDownList 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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'

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a DropDownList entry during edit David C ASP .Net 0 01-13-2009 04:12 PM
Adding Dynamic Controls to a DataGrid for use with the built in DataGrid Edit/Update functionality davidjgonzalez@gmail.com ASP .Net Datagrid Control 1 03-25-2006 06:55 AM
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM
Edit All Function for DataGrid, and Moving the Edit Function in a DataGrid Schultz ASP .Net 3 02-14-2005 04:47 AM



Advertisments