2

我有以下 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_RowUpdatinge.NewValues则不包含已编辑的值,而是包含旧的值。

我四处搜索,发现我可能不得不使用onRowUpdated事件,但后来我遇到了另一个问题;这个事件根本没有被触发。我试图设置e.Cancel = falsegridCar_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();
    }
}
4

2 回答 2

5

你确定你没有GridView对回发进行数据绑定吗?你应该只这样做if(!IsPostBack)

protected void Page_Load(Object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        BindGridView();
    }
}

否则,您将加载旧值并阻止该RowUpdated事件。

更新

我可能做错了很多事情,你能告诉我现在应该如何进行排序和更新吗?

这是一个完整的工作示例:

public partial class GridTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        var source = GetCarTable().Select("", this.SortExpression).CopyToDataTable();
        GridCar.DataSource = source;
        GridCar.DataBind();
    }

    private string SortExpression
    {
        get
        {
            if (ViewState["SortExpression"] == null || string.IsNullOrEmpty((String)ViewState["SortExpression"]))
            {
                ViewState["SortExpression"] = "CarModel ASC";
            }
            return ViewState["SortExpression"].ToString();
        }
        set { ViewState["SortExpression"] = value; }
    }

    private static DataTable GetCarTable()
    {
        DataTable carsTable = new DataTable("cars");
        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");

        return carsTable;
    }

    protected void gridCar_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
    {
        string currentSortColumn = null;
        string currentSortDirection = null;
        currentSortColumn = this.SortExpression.Split(' ')[0];
        currentSortDirection = this.SortExpression.Split(' ')[1];

        if (e.SortExpression.Equals(currentSortColumn))
        {
            //switch sort direction
            switch (currentSortDirection.ToUpper())
            {
                case "ASC":
                    this.SortExpression = currentSortColumn + " DESC";
                    break;
                case "DESC":
                    this.SortExpression = currentSortColumn + " ASC";
                    break;
            }
        }
        else
        {
            this.SortExpression = e.SortExpression + " ASC";
        }

        //load the data with this SortExpression and DataBind the Grid
        BindGrid();
    }

    protected void GridCar_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridCar.EditIndex = e.NewEditIndex;
        BindGrid();
    }

    protected void GridCar_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridCar.EditIndex = -1;
        BindGrid();
    }

    protected void GridCar_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var oldValues = e.OldValues;
        var newValues = e.NewValues;
        // BindGrid();
    }

    protected void GridCar_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //BindGrid();
    }
}

aspx 几乎保持不变:

<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"
            AutoGenerateColumns="true">
</asp:GridView>
于 2013-02-06T08:16:54.907 回答
0

您的代码没有 DataSourceID。我假设数据绑定在代码后面。

如果您只想编辑/更新 GridView 的行,您需要的是ItemUpdating事件(例如gridcar_ItemUpdating)而不是 RowUpdating

我希望它对你有帮助。

于 2013-02-06T08:28:16.067 回答