gpt4 book ai didi

javascript - GridView RowUpdating 在第二页中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 16:05:50 25 4
gpt4 key购买 nike

我使用以下代码在 OnUpdating 命令期间更新 gridview 中的值,

protected void grid_view_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)grid_view.Rows[e.RowIndex];
int number = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("flatnumber")).Text);
string type = ((DropDownList)row.Cells[4].FindControl("flattype")).Text;
int max = Convert.ToInt32(((DropDownList)row.Cells[5].FindControl("flatvacancy")).Text);
string flatID = ((Label)row.Cells[0].FindControl("flatid")).Text;
DataTable dt = (DataTable)ViewState["Table"];
DataRow[] datarow = dt.Select("ID='" + flatID + "'");
dt.Rows[e.RowIndex].BeginEdit();
dt.Rows[e.RowIndex]["Number"] = number;
dt.Rows[e.RowIndex]["Type"] = type;
dt.Rows[e.RowIndex]["Vacancy"] = max;
dt.Rows[e.RowIndex].EndEdit();
dt.AcceptChanges();
ViewState["Table"] = dt;
grid_view.EditIndex = -1;
grid_view.DataSource = dt;
grid_view.DataBind();
}

这段代码在 GridView 的第一页中工作正常,但是当我尝试编辑第二页时它不起作用。当我单击编辑按钮时,正在加载相应的行编辑模板。当我单击“保存”而不是保存到第二页时,第一页行会被修改。例如,如果我单击第二页第二行中的编辑,然后单击更新后,第一页的第三行将使用新值进行修改,而不是第二页的第二行。

我的 RowEditing 和 CancelEditing 代码如下,

protected void grid_view_RowEditing(object sender, GridViewEditEventArgs e)
{
grid_view.EditIndex = e.NewEditIndex;
DataTable dt = (DataTable)ViewState["Table"];
grid_view.DataSource = dt;
grid_view.DataBind();
}

protected void grid_view_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grid_view.EditIndex = -1;
DataTable dt = (DataTable)ViewState["Table"];
grid_view.DataSource = dt;
grid_view.DataBind();
}

最佳答案

您可以仅使用 e.RowIndex 访问更新的值,但要在数据表中进行更改,您必须添加 grid_view.PageIndexgrid_view 的乘积。 PageSize 更改为 e.RowIndex 以在实际行中进行更改。

protected void grid_view_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)grid_view.Rows[e.RowIndex];
int number = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("flatnumber")).Text);
string type = ((DropDownList)row.Cells[4].FindControl("flattype")).Text;
int max = Convert.ToInt32(((DropDownList)row.Cells[5].FindControl("flatvacancy")).Text);
string flatID = ((Label)row.Cells[0].FindControl("flatid")).Text;
DataTable dt = (DataTable)ViewState["Table"];
int row1=e.RowIndex+(grid_view.PageIndex*grid_view.PageSize)
dt.Rows[row1].BeginEdit();
dt.Rows[row1]["Number"] = number;
dt.Rows[row1]["Type"] = type;
dt.Rows[row1]["Vacancy"] = max;
dt.Rows[row1].EndEdit();
dt.AcceptChanges();
ViewState["Table"] = dt;
grid_view.EditIndex = -1;
grid_view.DataSource = dt;
grid_view.DataBind();
}

关于javascript - GridView RowUpdating 在第二页中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41889957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com