Hi Payalsh,
You don't need to hide the column. You need to hide the Approve button once the Status changed.
Use OnRowDataBound event and hide the Button as per the status. Please refer below sample.
Database
I have made use of the following table Students with the schema as follows.
I have already inserted few records in the table.
HTML
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="StudentId">
<ItemTemplate>
<asp:Label ID="lblStudentId" runat="server" Text='<%# Eval("StudentId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentName">
<ItemTemplate >
<asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate >
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="Approve" />
</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();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string status = (e.Row.FindControl("lblStatus") as Label).Text.ToUpper();
if (status == "P")
{
(e.Row.FindControl("btnApprove") as Button).Visible = false;
}
if (status == "A")
{
(e.Row.FindControl("btnApprove") as Button).Visible = true;
}
}
}
protected void Approve(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT StudentId, StudentName, Status FROM Students", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
this.gvStudents.DataSource = cmd.ExecuteReader();
this.gvStudents.DataBind();
con.Close();
}
}
}
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
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim status As String = (TryCast(e.Row.FindControl("lblStatus"), Label)).Text.ToUpper()
If status = "P" Then
TryCast(e.Row.FindControl("btnApprove"), Button).Visible = False
End If
If status = "A" Then
TryCast(e.Row.FindControl("btnApprove"), Button).Visible = True
End If
End If
End Sub
Protected Sub Approve(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGrid()
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT StudentId, StudentName, Status FROM Students", con)
cmd.CommandType = CommandType.Text
con.Open()
Me.gvStudents.DataSource = cmd.ExecuteReader()
Me.gvStudents.DataBind()
con.Close()
End Using
End Using
End Sub
Screenshot