Show Hide Edit and Delete Buttons in GridView using C# and VB.Net in ASP.Net

tanweeruddinbeeharry
 
on Oct 13, 2021 10:51 PM
Sample_721426.zip
424 Views

this is a column in the gridview

when i clicked on edit button i need to hide the other edit and delete buttons

I am using Rowdatabound

protected void gvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit)
    {                 
        ImageButton btn_edit = (ImageButton)e.Row.FindControl("LNK_EDIT");
        ImageButton btn_delete = (ImageButton)e.Row.FindControl("LNK_BTN_DELETE");
        btn_edit.Visible = false;
        btn_delete.Visible = false;
    }
}

When i debug it the btn_edit and btn_delete is null.

How to correct it?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 14, 2021 12:17 AM

Dear 

  kindly check below sample.

HTML

<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="CustomerId" />
        <asp:BoundField HeaderText="Name" DataField="Name" />
        <asp:BoundField HeaderText="Country" DataField="Country" />
        <asp:TemplateField HeaderText="Modify">
            <ItemTemplate>
                <asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="OnEdit" />
                <asp:Button ID="btnUpdate" runat="server" Text="Update" Visible="false" />
                <asp:Button ID="btnDelete" runat="server" Text="Delete" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

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

private void BindGrid()
{
    string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conn))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    gvCustomers.DataSource = dt;
                    gvCustomers.DataBind();
                }
            }
        }
    }
}

protected void OnEdit(object sender, EventArgs e)
{
    GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
    int index = row.RowIndex;
    for (int i = 0; i < gvCustomers.Rows.Count; i++)
    {
        if (index == i)
        {
            (gvCustomers.Rows[i].FindControl("btnUpdate") as Button).Visible = true;
            (gvCustomers.Rows[i].FindControl("btnEdit") as Button).Visible = false;
        }
        else
        {
            (gvCustomers.Rows[i].FindControl("btnEdit") as Button).Visible = false;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub

Private Sub BindGrid()
    Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conn)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    gvCustomers.DataSource = dt
                    gvCustomers.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub

Protected Sub OnEdit(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As GridViewRow = TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)
    Dim index As Integer = row.RowIndex
    For i As Integer = 0 To gvCustomers.Rows.Count - 1
        If index = i Then
            TryCast(gvCustomers.Rows(i).FindControl("btnUpdate"), Button).Visible = True
            TryCast(gvCustomers.Rows(i).FindControl("btnEdit"), Button).Visible = False
        Else
            TryCast(gvCustomers.Rows(i).FindControl("btnEdit"), Button).Visible = False
        End If
    Next
End Sub

Screenshot