积极答复者
gridview编辑更新

问题
答案
-
请确定你的Dropdownlist绑定的数据源和GridView的数据源完全一致(注意:数据源应该是两个数据源,一个绑定到Dropdownlist,另一个是GridView。但是它们的SQL语句必须完全一致,这样使得筛选出的数据必须保持同步!)。然后在Dropdownlist的SelectIndexChanged事件中:
GridView1.EditIndex = Dropdownlist.SelectedIndex; //因为数据源一致,所以对应的数据自然应该一致,Index也一致
GridView1.DataBind();
- 已编辑 ThankfulHeart 2012年3月12日 7:12
- 已标记为答案 彭123 2012年3月12日 7:14
全部回复
-
就是我想编辑222项,点了编辑时却变成了243项。我所谓的错位是这个意思。
我的代码是protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { SqlDataSource1.SelectCommand = "select * from 合同表 where 厂商名称='" + DropDownList1.SelectedItem + "' order by 序号 desc"; } protected void Button1_Click(object sender, EventArgs e) { SqlDataSource1.SelectCommand = "select * from 合同表 order by 序号 desc"; } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { SqlDataSource1.SelectCommand = "select * from 合同表 where 采购单号='" + DropDownList2.SelectedItem + "' order by 序号 desc"; //GridView1.DataBind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //SqlDataSource1.SelectCommand = "select * from 合同表 where 采购单号='" + DropDownList2.SelectedItem + "' order by 序号 desc"; }
-
请确定你的Dropdownlist绑定的数据源和GridView的数据源完全一致(注意:数据源应该是两个数据源,一个绑定到Dropdownlist,另一个是GridView。但是它们的SQL语句必须完全一致,这样使得筛选出的数据必须保持同步!)。然后在Dropdownlist的SelectIndexChanged事件中:
GridView1.EditIndex = Dropdownlist.SelectedIndex; //因为数据源一致,所以对应的数据自然应该一致,Index也一致
GridView1.DataBind();
- 已编辑 ThankfulHeart 2012年3月12日 7:12
- 已标记为答案 彭123 2012年3月12日 7:14
-
原因找到了,原来是在触发 GridView1_RowEditing时也要排序。不然就会错位。
谢谢楼上的各位。
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource1.SelectCommand = "select * from 合同表 where 采购单号='" + DropDownList2.SelectedItem + "' order by 序号 desc";
}还是这一句,不能少。
- 已建议为答案 ThankfulHeart 2012年3月12日 8:11