我有以下 GridView:
<asp:GridView ID="gridCar" runat="server" RowStyle-ForeColor="Black" AllowSorting="true" OnSorting="gridCar_Sorting" AutoGenerateEditButton="true" OnRowEditing="gridCar_RowEditing" OnRowCancelingEdit="gridCar_RowCancelingEdit" OnRowUpdating="gridCar_RowUpdating" OnRowUpdated="gridCar_RowUpdated"></asp:GridView>
我想更新编辑按钮选择的行。问题是,如果我尝试在 中执行此操作gridCar_RowUpdating
,e.NewValues
则不包含已编辑的值,而是包含旧的值。
我四处搜索,发现我可能不得不使用onRowUpdated
事件,但后来我遇到了另一个问题;这个事件根本没有被触发。我试图设置e.Cancel = false
,gridCar_RowUpdating
但这不会解决问题。
GridView 绑定到 DataTable。
编辑:这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : Page
{
DataTable carsTable = new DataTable("cars");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
carsTable.Columns.Add("CarID");
carsTable.Columns.Add("CarRegNum");
carsTable.Columns.Add("CarModel");
carsTable.Columns.Add("CarType");
carsTable.Columns.Add("CarOwner");
carsTable.Rows.Add(1, "AAA-111", "Toyota", "Hatchback", "Matti");
carsTable.Rows.Add(2, "BBB-222", "Mercedes-Benz", "Van", "Keijo");
carsTable.Rows.Add(3, "CCC-333", "Renault", "Regular", "Matilda");
carsTable.AcceptChanges();
gridCar.DataSource = carsTable;
gridCar.DataBind();
}
}
protected string dataViewSortDirection(SortDirection direction)
{
switch (direction)
{
case SortDirection.Ascending:
return "ASC";
case SortDirection.Descending:
return "DESC";
default:
throw new ArgumentOutOfRangeException();
}
}
protected void gridCar_Sorting(object sender, GridViewSortEventArgs e)
{
gridCar.Sort(e.SortExpression, e.SortDirection);
// update GridView
gridCar.DataBind();
}
protected void gridCar_RowEditing(object sender, GridViewEditEventArgs e)
{
gridCar.EditIndex = e.NewEditIndex;
gridCar.DataBind();
}
protected void gridCar_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridCar.EditIndex = -1;
gridCar.DataBind();
}
protected void gridCar_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
labelDebug.Text += "b";
//gridCar.EditIndex = -1;
//gridCar.DataBind();
}
protected void gridCar_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
labelDebug.Text += "a";
gridCar.EditIndex = -1;
gridCar.DataBind();
}
}