DateGrid分页出点问题
DateGrid带排序 带编辑删除 现在的情况是 我点后面几页某行的编辑 就自己跳到第一页了 比如说我点第二页第二行编辑就跳到第一页了,同时第一页第二行也成编辑状态了.我修改后就把所有页的第二行都修改了 请高手帮忙看看!谢谢Html
<asp:datagrid id="DataGrid1" runat="server" Width="908px" AllowPaging="True" Height="168px" AllowSorting="True"
AutoGenerateColumns="False" DataKeyField="id" OnEditCommand="DataGrid1_Edit" OnCancelCommand="DataGrid1_Cancel"
OnUpdateCommand="DataGrid1_Update" PageSize="2">
<Columns>
<asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="id"></asp:BoundColumn>
<asp:BoundColumn DataField="shop" ReadOnly="True" HeaderText="店别"></asp:BoundColumn>
<asp:BoundColumn DataField="LayoutName" ReadOnly="True" HeaderText="档期"></asp:BoundColumn>
<asp:BoundColumn DataField="GiftSale" ReadOnly="True" HeaderText="级别"></asp:BoundColumn>
<asp:BoundColumn DataField="GiftName" ReadOnly="True" HeaderText="名称"></asp:BoundColumn>
<asp:BoundColumn DataField="GiftNum" HeaderText="数量"></asp:BoundColumn>
<asp:BoundColumn DataField="GiftShipperSL" HeaderText="发货人"></asp:BoundColumn>
<asp:BoundColumn DataField="GiftConsignee" HeaderText="收货人"></asp:BoundColumn>
<asp:BoundColumn DataField="ShipmentDate" HeaderText="日期" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid></TD>
Cs
private void GetSqlData()
{//得到出库记录明细
string conStr = ConfigurationSettings.AppSettings["connString"];
SqlConnection cxconn=new SqlConnection( conStr);
string strsql="";
strsql= "select * from Gift_shipment where (Shop = '"+this.db.SelectedItem.Value+"') AND (LayoutName = '"+this.Layout.SelectedItem.Value+"') ORDER BY convert(int,GiftSale) ";
if(Ck.Checked==true)
{
strsql= "select * from Gift_shipment where (Shop = '"+db.SelectedItem.Value+"') AND (LayoutName = '"+Layout.SelectedItem.Value+"') AND (ShipmentDate='"+SDate.Text+"')ORDER BY ShipmentDate";
}
SqlDataAdapter cxAdapter = new SqlDataAdapter(strsql,cxconn);
cxconn.Open();
DataSet cxSet=new DataSet();
cxAdapter.Fill(cxSet,"Gift_shipment");
DataGrid1.DataSource=cxSet;
DataGrid1.DataKeyField="id";
DataGrid1.DataBind();
cxconn.Close();
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
try
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
}
catch
{
DataGrid1.CurrentPageIndex = 0;
}
GetSqlData();
}
protected void DataGrid1_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{//序号从0开始
string conStr = ConfigurationSettings.AppSettings["connString"];
// string strid=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
// string strid = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string strid = e.Item.Cells[0].Text.ToString();
string strdb = e.Item.Cells[1].Text.ToString();
string strLayout = e.Item.Cells[2].Text.ToString();
string strGiftSale = e.Item.Cells[3].Text.ToString();
string strGiftName = e.Item.Cells[4].Text.ToString();
string strGiftNum = ((TextBox)e.Item.Cells[5].Controls[0]).Text.ToString();
string strGiftShipperSL=((TextBox)e.Item.Cells[6].Controls[0]).Text.ToString();
string strGiftConsignee=((TextBox)e.Item.Cells[7].Controls[0]).Text.ToString();
string strDate=((TextBox)e.Item.Cells[8].Controls[0]).Text.ToString();
string updateCmd="UPDATE Gift_shipment set GiftName='"+strGiftName+"',LayoutName='"+strLayout+"',GiftSale='"+strGiftSale+"',ShipmentDate='"+strDate+"',GiftNum='"+strGiftNum+"',GiftShipperSL='"+strGiftShipperSL+"',GiftConsignee='"+strGiftConsignee+"' where id ='"+strid+"'";
SqlConnection con=new SqlConnection(conStr);
SqlCommand myCommand=new SqlCommand(updateCmd,con);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
DataGrid1.EditItemIndex=-1;
GetSqlData();
myCommand.Connection.Close();
}
catch
{
Response.Write("<script>alert('修改失败!')</script>");
}
} --------------------编程问答-------------------- 哦,有两点你可能搞错了,我没看你的代码,不过我听你叙述的我以前也出现过的:
一、是数据表的字段不唯一,造成你的修改动到了多行(我说的都是可能)
二、是你绑定的时候不要按原来的样子绑定,要按你查询出来后的那个griddata(待条件)的来绑定,就OK啦 --------------------编程问答-------------------- 帮顶吧 太长了 --------------------编程问答-------------------- 我是根据id来修改的不会照成字段不唯一,后面那种方法怎么实现呀? --------------------编程问答-------------------- 这个问题我以前好像也碰到过的,是数据绑定的关系! --------------------编程问答-------------------- 好好检查下绑定有问题吧 --------------------编程问答-------------------- 我又试了下,在第一页编辑就没问题,在后面几页就要出问题了
补充:.NET技术 , ASP.NET