(Product)gvPro.DataKeys[e.RowIndex].Value;Models.Product”的对象强制转换为类型“iPadMenu.Product” 报着个错Linq
        static void Delete(int id)
        {
            Console.WriteLine("-----------begin delete a Product");
            using (RestaurantDataContext db = new RestaurantDataContext())
            {
                db.Log = Console.Out;
                //取出student
                var student = db.Product.SingleOrDefault<Product>(s => s.Pro_Id == id);                if (student == null)
                {
                    Console.WriteLine("Product is null");
                    return;
                }                db.Product.DeleteOnSubmit(student);                db.SubmitChanges();
            }
            Console.WriteLine("------------end Delete Product");
        }
这样我怎么获到 参数的ID

解决方案 »

  1.   

    gvPro.DataKeys[e.RowIndex].Value 这个是个值  你为啥要转换成对象string id=gvPro.DataKeys[e.RowIndex].Value;
    Delete(id)
      

  2.   

    gvPro.DataKeys[e.RowIndex].Value表示主键值
    会为Produc对象??
    string id=gvPro.DataKeys[e.RowIndex].Value;
      

  3.   

    gvPro.DataKeys[e.RowIndex].Value =>这个是一个具体的值呢,怎么可以转成对象呢????
      

  4.   


    Models.Product”的对象强制转换为类型“System.IConvertible”报这个错
      

  5.   


      protected void gvPro_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {  
                int id=Convert.ToInt32(gvPro.DataKeys[e.RowIndex].Value);
                
                //Delete(id);
                tabnind();
            }无法将类型为“Models.Product”的对象强制转换为类型“System.IConvertible”。说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 无法将类型为“Models.Product”的对象强制转换为类型“System.IConvertible”。源错误: 
    行 29:         protected void gvPro_RowDeleting(object sender, GridViewDeleteEventArgs e)
    行 30:         {  
    行 31:             int id=Convert.ToInt32(gvPro.DataKeys[e.RowIndex].Value);
    行 32:             
    行 33:             //Delete(id);