Grdiview last row Delete Button disable functionality is not working for Gridview Edit button

Last Reply on May 22, 2014 04:33 AM By Azim

Posted on May 22, 2014 04:31 AM

In one of my Web Page, there is one Modal Pop up, in which I am using Gridview Edit, Update, CancelEdit, Delete functionality.

Main functionality is : If while deleting any of the Gridview Row, if only 1 row remains in Gridview (that row can be any row), then that row should not get deleted.

Rest of Edit, Update, CancelEdit functionaly is working fine.

I used below code for above:

 C#:

protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {                    
                BindGrid();
                IsLastRow();
            }
        }

        protected void BindGrid()
        {
            string id = Request.QueryString["Id"].ToString();

            string query = "SELECT  X, Y FROM Table where ID='" + id + "'";
            this.GridView1.DataSource = DataManager.DatabaseManager.GetOrCreateConnection(DataManager.DatabaseManager.ConnectionType.MySQL).GetData(query);
            this.GridView1.DataBind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        //enable edit
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {            
            GridView1.EditIndex = e.NewEditIndex;               
            BindGrid();            
        }

        //enable update
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                
                int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
                TextBox TxtX = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TxtX");
                TextBox TxtY = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TxtY");

                string query = "update sgwebdb.goal_function_value SET X='" + TxtX.Text + "', Y='" + TxtY.Text + "' where GFM_ID='" + id + "'";
                DataManager.DatabaseManager.GetOrCreateConnection(DataManager.DatabaseManager.ConnectionType.MySQL).ExecuteNonQueryCommand(query, null);
                GridView1.EditIndex = -1;
                BindGrid();
                Response.Redirect(Request.Url.AbsoluteUri);
                
            }
            catch (Exception ex)
            {
            }
        }

        //enable cancel edit
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {              
            GridView1.EditIndex = -1;
            BindGrid();
            Response.Redirect(Request.Url.AbsoluteUri);            
        }

        //enable delete
        protected void lnldelete_Click(object sender, EventArgs e)
        {
            try
            {
                string username = (string)Session["UserAuthentication"];
                LinkButton lnkbtn = sender as LinkButton;
                GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;

                int id = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());

                string CommandText = "UPDATE sgwebdb.goal_function_value SET DeletionDate = SYSDATE() , DeletedBy = '" + username + "' WHERE GFM_ID = '" + id + "'";
                //string CommandText = "delete from sgwebdb.goal_function_value where GFM_ID='" + id + "'";
                object r = new object();
                r = DataManager.DatabaseManager.GetOrCreateConnection(DataManager.DatabaseManager.ConnectionType.MySQL).ExecuteScalerCommand(CommandText, null);
                int result = Convert.ToInt32(r);
                if (result == 0)
                {
                    BindGrid();
                }
                Response.Redirect(Request.Url.AbsoluteUri);
            }
            catch (Exception ex)
            {
            }
        }

        //code to disable gridview remaining last row
        private void IsLastRow()
        {
            if (this.GridView1.Rows.Count == 1)
            {
                (this.GridView1.Rows[0].FindControl("lnldelete") as LinkButton).Enabled = false;
                (this.GridView1.Rows[0].FindControl("lnldelete") as LinkButton).ForeColor = Color.Blue;
            }
        }

HTML:

<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false" DataKeyNames="ID" OnPageIndexChanging="GridView1_PageIndexChanging"
      OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
        <Columns>

            <asp:TemplateField HeaderText="Node X">
                <ItemTemplate>
                    <asp:Label ID="LblX" runat="server" Text='<%#Eval("X") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>                    
                    <asp:TextBox ID="TxtX" runat="server" Text='<%#Eval("X") %>' MaxLength="4"></asp:TextBox>                    
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Node Y">
                <ItemTemplate>
                    <asp:Label ID="LblY" runat="server" Text='<%#Eval("Y") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>                   
                    <asp:TextBox ID="TxtY" runat="server" Text='<%#Eval("Y") %>' MaxLength="5"></asp:TextBox>                   
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkedit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="lnkupdate" runat="server" CommandName="Update">Update</asp:LinkButton>
                    <asp:LinkButton ID="lnkcancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
           
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:LinkButton ID="lnldelete" runat="server" OnClick="lnldelete_Click">Delete</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>

problem is: When any last row is left in Gridview, its Delete button is disables that time (as per requirement and functinality).
But when user clicks on Gridview "Edit" button, update section opens, and at that time "Delete" button gets Enabled, so that time user can delete the last row of Gridview which should not be done.

I had fixed this problem for "Cancel" and "Update" button of Gridview using below line:

Response.Redirect(Request.Url.AbsoluteUri);

but unable to fix it for "Edit" button of Gridview

Please suggest some solution so that if Last row is left in Gridview , and user clicks on Gridview "Edit" button of that row, then at that time "Delete" button should not get "enabled" it should remain "disabled"

Please reply.

Posted on May 22, 2014 04:33 AM

Call the IsLastRow finction in RowEditing like this

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    this.GridView1.EditIndex = e.NewEditIndex;
    this.BindGrid();
    this.IsLastRow();
}