日期:2014-05-16  浏览次数:20441 次

gridview显示查询的内容,想要编辑时出错!
   首先绑定一个表的数据到gridview中,然后按条件查询到的结果显示在gridview中,对查询到的结果中的某条数据点击编辑时,却出现了整个表的数据,而不是查询到的内容。也就是无法根据查询到的结果直接对数据进行修改。

------解决方案--------------------
错误提示贴出来啊!正常情况下是可以的~你显示的数据是不是用的视图?如果是的话,就不能编辑
------解决方案--------------------
点击编辑是数据源是同一个吗?或者调用的是同一个数据源吗?

------解决方案--------------------
"select  *  from Insurance  where ID='" + Convert.ToInt32(TextBox5.Text.Trim()) + "' order by ID DESC";
 string number = GridView1.DataKeys[e.RowIndex].Value.ToString();
 
我猜测是这两句的问题

首先,你在查询的时候是倒序排列,这个时候你的6在第一条,1在最后一条
当你点击编辑获取当前行的时候,你通过的是GridView1.DataKeys[e.RowIndex].Value.ToString();来获取的,这个时候GridView没有排序,也就是说1在第一条,6在最后一条,但是你的数据源没更新,所以你看到的还是6在第一条

解决方式:不要直接用GridView1.DataKeys[e.RowIndex].Value.ToString();获取number,改用绑定一列隐藏的Label来解决
------解决方案--------------------
引用:
按升序来排列,没用变化。
用label的话,不知道该如何把label的值转换成string或者int类型。试了了网上的一些方法,还是报错。

不是升序的问题,我是猜测你用 GridView1.DataKeys[e.RowIndex].Value.ToString();它默认的是没有序列的,当然仅仅是猜测,我也没用过,不敢妄下结论

使用Label报了什么错误?还是之前的情况吗?
------解决方案--------------------
试试GridView1有个DatakeyNames属性绑定ID列,在后台 int id = Convert.ToInt32(e.CommandArgument);

------解决方案--------------------
我了个去,你也不看看你表的主键是什么,是ID,你在编辑的时候怎么用number了?
string number = GridView1.DataKeys[e.RowIndex].Value.ToString(); 把这句改成:
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
 string sql = "update Insurance set Type='" + type + "',Company='" + company + "',Monkey='" + monkey + "',Date='" + date + "',Time='" + time + "' where Number='" + number + "'"; 把这句改成:
 string sql = "update Insurance set Type='" + type + "',Company='" + company + "',Monkey='" + monkey + "',Date='" + date + "',Time='" + time + "' where ID='" + id+ "'";
然后再gridview属性栏里DataKeys属性添加id
问题就解决了。纯碎是你没用主键值,所以你点击编辑的时候才会出现编辑行错误。
------解决方案--------------------
引用:
我了个去,你也不看看你表的主键是什么,是ID,你在编辑的时候怎么用number了?
string number = GridView1.DataKeys[e.RowIndex].Value.ToString(); 把这句改成:
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
 string sql = "update Insurance set Type='" + type + "',Company='" + company + "',Monkey='" + monkey + "',Date='" + date + "',Time='" + time + "' where Number='" + number + "'"; 把这句改成:
 string sql = "update Insurance set Type='" + type + "',Company='" + company + "',Monkey='" + monkey + "',Date='" + date + "',Time='" + time + "' where ID='" + id+ "'";
然后再gridview属性栏里DataKeys属性添加id
问题就解决了。纯碎是你没用主键值,所以你点击编辑的时候才会出现编辑行错误。

犀利,看了这么久我才发现主键是ID