日期:2014-05-18  浏览次数:20473 次

GridView中删除问题
在GridView中添加了一个删除列,点击删除记录。为什么不行啊?vs2005版   代码如下:
        ClassDataBase   cdb   =   new   ClassDataBase();
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                BindData();
        }

        protected   void   BindData()
        {
                string   sqlstr   =   "select   *   from   GPSInfor ";
                DataSet   ds   =   cdb.selectData(sqlstr, "GPSTable ");
                this.GridView1.DataSource   =   ds;
                this.GridView1.DataKeyNames   =   new   string[]   {   "id "   };
                this.GridView1.DataBind();
        }
        protected   void   GridView1_RowDeleting(object   sender,   GridViewDeleteEventArgs   e)
        {
                SqlConnection   cn   =   new   SqlConnection(ConfigurationManager.AppSettings[ "conStr "]);
                cn.Open();

                SqlCommand   cm   =   new   SqlCommand( "delete   from   GPSInfor   where   id= ' "   +   this.GridView1.DataKeys[e.RowIndex].Values.ToString()   +   " ' ",   cn);
                        cm.ExecuteNonQuery();
                        Response.Write( " <script   language=javascript> alert( '删除成功! ') </script> ");
                        cn.Close();
                        BindData();
          }

错误提示如下:

将   varchar   值   'System.Collections.Specialized.OrderedDictionary '   转换为数据类型为   int   的列时发生语法错误。  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.Data.SqlClient.SqlException:   将   varchar   值   'System.Collections.Specialized.OrderedDictionary '   转换为数据类型为   int   的列时发生语法错误。

不懂,高手指教啊。

------解决方案--------------------
GridView1.DataKeys[e.RowIndex].Values不就是INT?
为什么要GridView1.DataKeys[e.RowIndex].Values.ToString(),
this.GridView1.DataKeyNames = new string[] { "id " };
你的id是int的吧!

------解决方案--------------------
SqlCommand cm = new SqlCommand( "delete from GPSInfor where id= ' " + this.GridView1.SelectedValue.ToString() + " ' ", cn);

------解决方案--------------------
SqlCommand c