DropDownList has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Attempt to find the value in the drop down list before attempting to set the SelectedValue, like this:
if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
}
Note: The Trim()
call will remove any leading or trailing spaces in your text box text, which could be a cause for a match not being found.
So your full code should be this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
GridViewRow row = GridView1.SelectedRow;
AccountNumber.Text = (string)row.Cells[0].Text;
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
it's so simple just use this
Dropdown.SelectedValue = null;
Dropdown.DataBind();
Hi maybe in that cell from your gridview have white space or dropdownlist have white space for example isn't the same this
Dolar__ = Dolar
or
Dolar = Dolar__
use a Trim in code behind to clear white spaces in SQL Server don't use Rtrim this isn't good practices