Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get SelectedValue from dropdownlist in GridView
Message
From
16/03/2010 08:36:25
 
 
To
16/03/2010 08:19:04
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01454691
Message ID:
01454735
Views:
23
>>>Hi,
>>>
>>>I'm trying to add/edit some data in a GridView and need to access the SelectedValue of a drop downlist that is in the grid.
>>>
>>>My code is this:
>>>
>>>
GridViewRow gvRow = this.grdPhones.Rows[e.RowIndex];
>>>Guid PhoneType = new Guid(((DropDownList)(gvRow.Cells[1].Controls[0])).SelectedValue.ToString());
>>>
>>>And the error I get is this:
>>>
>>>System.InvalidCastException was unhandled by user code
>>> Message="Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DropDownList'."
>>> Source="SamaanSystems.IAS.Web"
>>> StackTrace:
>>> at SamaanSystems.IAS.Web.ClientEdit.grdPhones_RowUpdating(Object sender, GridViewUpdateEventArgs e) in C:\Development\Mere Mortals .NET Framework 2008\SamaanSystems\IAS.Web\ClientEdit.aspx.cs:line 528
>>> at System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e)
>>> at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
>>> at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
>>> at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
>>> at OakLeaf.MM.Main.Web.UI.WebControls.mmGridView.OnBubbleEvent(Object source, EventArgs e)
>>> at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
>>> at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
>>> at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
>>> at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
>>> at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
>>> at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
>>> at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
>>> at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
>>> at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
>>> InnerException:
>>>
>>>This shouldn't be so hard :(
>>
>>The line looks right (there's an unneccessary pair of brackets - but no harm in that). The obvious implication is that 'gvRow.Cells[1].Controls[0]' is not actually a DropDownList ?
>
>Thanks Viv!
>
>the obvious was not so obvious at the end of a long day fighting with .NET :) The dropdownlist was in Controls[1].
>
>Is there a better or more generic way to find the column which is bound to a particular field in a dataset and then find the drop down list value? I hate hard coding in column and cell numbers.

GridViewRow inherits the .FindControl() method - maybe that would work (haven't got anything handy to test it I'm afraid).....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform