请解决错误ddlcityEdit'有一个无效的SelectedValue,因为它在项目列表中不存在。 [英] please solve the error ddlcityEdit' has a SelectedValue which is invalid because it does not exist in the list of items.

查看:83
本文介绍了请解决错误ddlcityEdit'有一个无效的SelectedValue,因为它在项目列表中不存在。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码文件



This is my code file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Details : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    Data_Acess da = new Data_Acess();
    Register rg = new Register();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            Display();
        }

    }
    private void Display()
    {
        var query = from c in db.Registers where c.Name == Convert.ToString(Session["Name"]) select c;
        GridView1.DataSource = query;
      GridView1.DataBind();
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Display();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Display();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        


        int i = e.RowIndex;
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"]);

        //  string stname = ((TextBox)GridView1.Rows[i].Cells[0].Controls[0]).Text;
        TextBox txtname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnameEdit");
        TextBox txtmob = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmobileEdit");
        TextBox txtAdd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddressEdit");
        DropDownList txtcity = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlcityEdit");
        TextBox txtgender = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtgenderEdit");
        TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtemailEdit");
        TextBox txtdob = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdateEdit");


     da.BindDropdownGrid(txtcity);


        Register rg = (from c in db.Registers where c.Id == ID select c).FirstOrDefault();
        rg.Name = txtname.Text;
        rg.Mobile = Convert.ToInt32(txtmob.Text);
        rg.Address = txtAdd.Text;
        rg.City = txtcity.SelectedItem.ToString();
        rg.Gender = txtgender.Text;
        rg.Email = txtemail.Text;
        rg.Dateofbirth = txtdob.Text;

        db.SubmitChanges();
        GridView1.EditIndex = -1;
        GridView1.DataBind();

        Response.Redirect("LoginForm.aspx");


    }
}





我是绑定下拉列表gridview使用linq和绑定代码在数据Acess层,如下所示:



i am binding dropdown list in gridview using linq and the bining code is in data Acess layer as shown below:

public void BindDropdownGrid(DropDownList ddlcityEdit)
    {
        ddlcityEdit.DataSource = from c in db.City_Names select c;
        ddlcityEdit.DataValueField = "Id";
        ddlcityEdit.DataTextField = "City";
        ddlcityEdit.DataBind();
    }

推荐答案

显然,您的下拉列表没有您要分配的值。



Clearly, your dropdownlist doesn't have the value you are trying to assign.

rg.City = txtcity.SelectedItem.ToString();





将此行替换为



Replace this line with

if(txtcity.SelectedIndex>=0)
{
    //rg.City = txtcity.SelectedItem.ToString();
    //don't do this. Instead use SelectedValue property

    rg.City = txtcity.SelectedValue.ToString();
}





这将停止抛出错误。但是,你的工作尚未完成。您需要确保在此行之前填写了下拉列表,以便在此行中分配项目。



如果这不能解决您的问题请告诉我:))



This will stop throwing the error. But, your job is not finished yet. You need to make sure that the you have filled the dropdownlist before this line so that there will be items to assign in this line.

In case this doesn't solve your problem please let me know :)


您好,



您可以通过两种方式获取下拉列表值。 />


1.
Hi,

You can get the dropdown list value in 2 ways.

1.
ddl.SelectedItem.Text



2.


2.

ddl.SelectedValue





使用其中任何一个并解决您的问题。



谢谢

Sisir Patro



Use any one of them and get your problem resolved.

Thanks
Sisir Patro


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           if ((e.Row.RowState & DataControlRowState.Edit) > 0)
           {
               DropDownList ddlcity = (DropDownList)e.Row.FindControl("ddlcityEdit");
               da.BindDropdownGrid(ddlcity);
           }
       }
   }







i已添加此代码它有效!!!




i have added this code and it worked!!!


这篇关于请解决错误ddlcityEdit'有一个无效的SelectedValue,因为它在项目列表中不存在。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆