Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handle ArgumentOutOfRangeException from DropDownList in Gridview 1

Status
Not open for further replies.

kvang

Programmer
Oct 7, 2005
129
US
How do you handle the ArgumentOutOfRangeException thrown from a DropDownList within a gridview? I have a editable field containing a DropDownList. The list is bind to a data source. Basically, the error is thrown because the item no longer exists in the list. When this exception occurs, I just want to set the selected value to something like "Not Applicable" which is a value that will always be in the list. Here is my code:

Code:
<asp:TemplateField HeaderText="Department" >
	<ItemTemplate>
		<asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
	</ItemTemplate>
	<EditItemTemplate>
		<asp:DropDownList ID="ddlDepartments" runat="server" 
			DataSourceID="ObjectDataSource2" 
			DataTextField="Department" 
			DataValueField="Department" OnDataBinding=""
			SelectedValue='<%# Bind("Department") %>'>                              
		</asp:DropDownList>
	</EditItemTemplate>                        
</asp:TemplateField>
 
your problem is [tt]DataSourceID="ObjectDataSource2"[/tt]. data source controls are evil. among the many reasons why; there are no hooks to manipulate the data between the database and the UI. therefore you will not be able to apply the logic you want.

replace the data source control with actual code. in the grid view row data bound event get the drop down list, and bind the data. then set the selected index accordingly.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks for the tip. It led me the right solution.

Code:
<asp:TemplateField HeaderText="Department" >
	<ItemTemplate>
		<asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
	</ItemTemplate>
	<EditItemTemplate>
		<asp:HiddenField ID="hfDepartment" runat="server" Value='<%# Bind("Department") %>' />
		<asp:DropDownList ID="ddlDepartments" runat="server" 
			DataTextField="Department" 
			DataValueField="Department">                              
		</asp:DropDownList>
	</EditItemTemplate>                        
</asp:TemplateField>

protected void gvwDocCats_RowDataBound(object sender, GridViewRowEventArgs e)
{
	if (e.Row.RowType == DataControlRowType.DataRow)
	{
		if (e.Row.RowIndex == gvwDocCats.EditIndex)
		{
			HiddenField hfDept = (HiddenField)e.Row.FindControl("hfDepartment");
			string currentDept = hfDept.Value;

			DropDownList ddl = (DropDownList)e.Row.FindControl("ddlDepartments");
			ddl.DataSourceID = "ObjectDataSource2";                    
			try
			{
				ddl.SelectedValue = currentDept;
				ddl.DataBind();
			}
			catch (ArgumentOutOfRangeException ex)
			{
				ddl.SelectedValue = "Not Applicable";
				ddl.DataBind();
			}
		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top