Adding Drop Down List to DataGrid

I know how to add controls to a datagrid using the asp:templatecolumn/itemtemplate, but I am unsure on how to do anything with it from there. I don’t have OnCommand, CommandName, or CommandArgument available to the drop down list so I don’t know how to identify which row was selected or what action to take.

Thanks

Can you explain what it is you want to do exactly?

For each row in the datagrid, I’d like a drop down list with options to Remove, Archive, Print, etc… Normally, I use buttons and set the CommandName to what the button does and the CommandArgument to the record id for that row. And then in the OnCommand event handler I place a switch statement based on the command name.

Nevermind, I think I may have just gotten it. I can use the OnSelectedIndexChanged event handler in place of the OnCommand, the item value property in place of CommandArgument, and the item text property in place of the CommandName.

The only downside is I don’t know how I would reset the selected property back to the orginal without re-populating the datagrid.

I thought I’d reply in case it wasn’t realized that there was still a question here: how can I reset the dropdownlist in a datagrid without re-populating the list?

assuming that by reset you mean selecting the first value in the dropdownlist.

try


ddlBox.SelectedIndex = 0;

Perhaps I need to get some code down and show an example. The drop down list is inside a datagrid so I don’t think I can access it directly.

Your right, you’ll need to use FindControls. Here is a article from 4guys. http://www.4guysfromrolla.com/webtech/050801-1.2.shtml

Also Marcie Robillard (AKA Datagrid Girl!) has gathered together a huge list of articles/links that are dedicated to the DataGrid. http://www.datagridgirl.com/default.aspx

Getting right to the point, this might be a working model of what you’re looking for?

http://aspnet.4guysfromrolla.com/demos/dgExample16.aspx

I will look at the above links, but here some code from what I am trying to do. I am not working in the edit item template, though.


Sub ddlAction_OnChange(sender As Object, e As System.EventArgs)
  'action based on drop down choice and record id
 End Sub
 
'inside datagrid
<asp:templatecolumn>
   <itemtemplate>
	<div align="right">
	 <asp:dropdownlist ID="ddlAction" runat="server" CssClass="dropdownlist" OnSelectedIndexChanged="ddlAction_OnChange" AutoPostBack="true">
	  <asp:listitem Text="select an option" Value="0" />
	  <asp:listitem Text="[modify]" />
	  <asp:listitem Text="[delete]" />
	  <asp:listitem Text="[view]" />
	  <asp:listitem Text="[add problem/issue]" />
	  <asp:listitem Text="[add outreach/referral]" />
	  <asp:listitem Text="[add treatment note]" />
	 </asp:dropdownlist>
	</div>
   </itemtemplate>
  </asp:templatecolumn>

No, those links really didn’t help. They center around using the drop down in edit mode.

What you need to do is aet a DataKey property for the grid, being the id field in the datasource.

Then you can get to that stuff in your event handler.

Here is a sample in C#, sorry no VB.NET example, maybe some vb guy can translate.


 		protected void ddlAction_OnChange(object sender, System.EventArgs e)
 		{
 			DropDownList ddl = ((DropDownList)sender);
 			string sSelectedValue = ddl.SelectedValue;
 			string sDataKey = oDG.DataKeys[((DataGridItem)ddl.NamingContainer).ItemIndex].ToString();
 		    System.Diagnostics.Debug.WriteLine("Selected Value = " + sSelectedValue.ToString());
 		    System.Diagnostics.Debug.WriteLine("Selected ID = " + sDataKey.ToString());
 		}
 

In my case it was the au_id field. And its a varchar.


 						    		<asp:DataGrid id="oDG" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"
 		    	Height="264px" Width="504px" AutoGenerateColumns="False" DataKeyField="au_id">
 

Here’s the translated VB.NET code:

 		
Protected Sub ddlAction_OnChange(s As Object, e As EventArgs)
     Dim ddl As DropDownList = CType(s, DropDownList)
     Dim sSelectedValue As String = ddl.SelectedValue
     Dim sDataKey As String = oDG.DataKeys(e.Item.ItemIndex)
     System.Diagnostics.Debug.WriteLine("Selected Value = " & sSelectedValue.ToString())
     System.Diagnostics.Debug.WriteLine("Selected ID = " & sDataKey.ToString())
End Sub

Has not been tested however so you may have to do some debugging.

Sweet, thanks for doing that.

You going to get any of that South Swell on tap for midweek, Zak?

I really, really apperciate the help, but that is returning an error -
Compiler Error Message: BC30456: ‘Item’ is not a member of ‘System.EventArgs’.

e doesn’t contain a property for Item, Zak didn’t get the translation right, although he did say it was untested.

If you look at my code and his, I used the NamingContainer approach and it has a ItemIndex property. e is just System Args not DataGrid args.


 	Protected Sub ddlAction_OnChange(ByVal s As Object, ByVal e As EventArgs)
 		Dim ddl As DropDownList = CType(s, System.Web.UI.WebControls.DropDownList)
 		Dim sSelectedValue As String = ddl.SelectedValue
 		Dim dgi As DataGridItem = CType(ddl.NamingContainer, System.Web.UI.WebControls.DataGridItem)
 		Dim sDataKey As String = oDG.DataKeys(dgi.ItemIndex)
 	    System.Diagnostics.Debug.WriteLine("Selected Value = " & sSelectedValue.ToString())
 		System.Diagnostics.Debug.WriteLine("Selected ID = " & sDataKey.ToString())
 	End Sub
 

Again, thanks for all the help - I really appreciate it.

I am running into a couple other problems, however, it is with my logic in using this approach, not the actual script.

[list][]The screen flickers three times. It’s not only annoying, but I have javascript written within that sub to open a new window for certain options so it loses focus (it actually posts back after writing the script so window.focus() isn’t doing me any good).
[
]I don’t have a way to confirm the delete option.
[/list]I think my best bet is to add a button to ‘submit’ the drop down list option, however, I don’t know how I would access the drop down list from another control.

I attached the full source if it helps - the drop down sub is towards the bottom of the script block.

Thanks again.

Just wondering if anyone had any follow-up thoughts to this.

Man, I pulled dawn patrol on Friday at Tamarack’s in Carlsbad. Sets were coming in 4-5 feet consistantly. Unfortunately, I was freezing my *** off when I got out of the car so I decided not to paddle out. There’s another SW swell supposed to come in this Thursday, I might paddle out then. Did you see any action from the last swell?

Sorry man, that script is way to long for me to weed through on my time budget but as far as your Confirm question is concerned, have you looked at this link?

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2405&lngWId=10