Hi lingers,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE Grid
(
ID INT IDENTITY PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Score INT,
Status VARCHAR(10),
Location VARCHAR(10)
)
INSERT INTO Grid VALUES('Kenneth',23,100,'Completed','OFFICE')
INSERT INTO Grid VALUES('John',28,300,'Pending','TRANSIT')
INSERT INTO Grid VALUES('Romeo',35,200,'Completed','OFFICE')
INSERT INTO Grid VALUES('Jude',56,400,'Pending','TRANSIT')
SELECT * FROM Grid
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound"
Font-Names="Arial" Font-Size="Small" Height="215px" Width="543px">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:BoundField DataField="Score" HeaderText="Score" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="location" HeaderText="Location" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCompleted" Text="Done" runat="server" />
<asp:Button ID="btnPending" Text="Send Back" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="green" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT Name,Age,Score,Status,Location FROM Grid ", con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("lblCompleted") as Label;
Button btn = e.Row.FindControl("btnPending") as Button;
if (e.Row.Cells[3].Text.Trim().ToLower() == "completed")
{
lbl.Visible = true;
btn.Visible = false;
}
if (e.Row.Cells[3].Text.Trim().ToLower() == "pending")
{
lbl.Visible = false;
btn.Visible = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindUserDetails()
End If
End Sub
Protected Sub BindUserDetails()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT Name,Age,Score,Status,Location FROM Grid ", con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lbl As Label = TryCast(e.Row.FindControl("lblCompleted"), Label)
Dim btn As Button = TryCast(e.Row.FindControl("btnPending"), Button)
If e.Row.Cells(3).Text.Trim().ToLower() = "completed" Then
lbl.Visible = True
btn.Visible = False
End If
If e.Row.Cells(3).Text.Trim().ToLower() = "pending" Then
lbl.Visible = False
btn.Visible = True
End If
End If
End Sub
Screenshot