locked
Cell value is always empty in RowCancelingEdit event in GridView RRS feed

  • Question

  • User1904516115 posted
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                <Columns>
                    <asp:CommandField ShowEditButton="true" />
                </Columns>
            </asp:GridView>
        </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public class EmployeeDetail
    {
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsSenior { get; set; }
    
    }
    
    public partial class _Default : System.Web.UI.Page
    {
        private List<EmployeeDetail> GetEmployees()
        {
            List<EmployeeDetail> list = new List<EmployeeDetail>();
            list.Add(new EmployeeDetail() { EmployeeID = 1, FirstName = "A", LastName = "B" });
            list.Add(new EmployeeDetail() { EmployeeID = 2, FirstName = "C", LastName = "D" });
            list.Add(new EmployeeDetail() { EmployeeID = 3, FirstName = "E", LastName = "F" });
            list.Add(new EmployeeDetail() { EmployeeID = 4, FirstName = "G", LastName = "H" });
            list.Add(new EmployeeDetail() { EmployeeID = 5, FirstName = "I", LastName = "J" });
            return list;
        }
    
        private void BindGrid()
        {
            GridView1.DataSource = GetEmployees();
            GridView1.DataBind();
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGrid();
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            string id = row.Cells[1].Text;//ALWAYS EMPTY
            Response.Write("You cancelled the edit operation for employee with id: " + id);
            GridView1.EditIndex = -1;
            BindGrid();
    
        }
    }
    
    

    Monday, February 4, 2019 3:27 AM

All replies

  • User1724605321 posted

    Hi vinodkpasi ,

    You can get the cell value by ;

     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridViewRow row = GridView1.Rows[e.RowIndex];
                string id = ((TextBox)row.Cells[1].Controls[0]).Text;
    
                Response.Write("You cancelled the edit operation for employee with id: " + id);
                GridView1.EditIndex = -1;
                BindGrid();
    
            }

    Result :

    Best Regards,

    Nan Yu

    Monday, February 4, 2019 5:14 AM
  • User-1716253493 posted

    Use DataKeyNames

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="EmployeeID" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit">
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            string EmployeeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        }

    Monday, February 4, 2019 6:51 AM
  • User-1038772411 posted

    Please place below code in your grid cancel event :

    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    TextBox id = (TextBox)row.Cells[1].Controls[0];
    var data = id.Text;
    Response.Write("You cancelled the edit operation for employee with id: " + data);
    GridView1.EditIndex = -1;
    BindGrid();

    Monday, February 4, 2019 8:15 AM