如何解决这个问题' DropDownList1'具有一个无效的SelectedValue,因为它不存在于项列表中。参数名称:value

时间:2016-04-06 12:53:37

标签: c# asp.net

public partial class allfiles : System.Web.UI.Page
{

received re = new received();
catgories cat = new catgories();
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    { 
      DropDownList1.DataSource = cat.get_all_cat();
      DropDownList1.DataTextField = "اسم المستند";
      DropDownList1.DataValueField = "رقم المستند";
      DropDownList1.DataBind();
    }
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
    // DropDownList1.Items.Clear();
   // DropDownList1.SelectedIndex = 0;
    TextBox10.Text = GridView1.SelectedRow.Cells[2].Text;
    TextBox1.Text = GridView1.SelectedRow.Cells[8].Text;
    TextBox2.Text = GridView1.SelectedRow.Cells[9].Text;
    TextBox3.Text = GridView1.SelectedRow.Cells[10].Text;
    TextBox11.Text = GridView1.SelectedRow.Cells[11].Text;
    TextBox4.Text = GridView1.SelectedRow.Cells[12].Text;
    TextBox5.Text = GridView1.SelectedRow.Cells[6].Text;
    TextBox6.Text = GridView1.SelectedRow.Cells[7].Text;


    DropDownList1.SelectedValue = GridView1.SelectedRow.Cells[3].Text;



    TextBox8.Text = GridView1.SelectedRow.Cells[4].Text;
    TextBox9.Text = GridView1.SelectedRow.Cells[5].Text;

}

选择行时会显示以下错误:

  

' DropDownList1'有一个SelectedValue,它是无效的,因为它确实如此   项目列表中不存在。参数名称:值

1 个答案:

答案 0 :(得分:1)

很好,我找到了解决方案 这段代码

    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(GridView1.SelectedRow.Cells[3].Text));

它有效