Hi RichardSa,
Please refer below sample.
SQL
CREATE TABLE [OnlineUserStatus]
(
[User] VARCHAR(50),
[Status] INT
)
INSERT INTO [OnlineUserStatus] VALUES('John Hammond', 0)
INSERT INTO [OnlineUserStatus] VALUES('Mudassar Khan', 1)
INSERT INTO [OnlineUserStatus] VALUES('Suzanne Mathews', 1)
INSERT INTO [OnlineUserStatus] VALUES('Robert Schidner', 0)
HTML
<asp:GridView ID="gvStatus" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="User" HeaderText="User" />
<asp:BoundField DataField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindStatus();
}
}
private void BindStatus()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT [User], [Status] FROM OnlineUserStatus", con))
{
con.Open();
this.gvStatus.DataSource = cmd.ExecuteReader();
this.gvStatus.DataBind();
con.Close();
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
int status = int.Parse(e.Row.Cells[1].Text);
if(status == 1)
{
e.Row.Cells[1].Text = "Active";
e.Row.Cells[1].BackColor = Color.Green;
}
else
{
e.Row.Cells[1].Text = "Inactive";
e.Row.Cells[1].BackColor = Color.Red;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindStatus()
End If
End Sub
Private Sub BindStatus()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT [User], [Status] FROM OnlineUserStatus", con)
con.Open()
Me.gvStatus.DataSource = cmd.ExecuteReader()
Me.gvStatus.DataBind()
con.Close()
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 status As Integer = Integer.Parse(e.Row.Cells(1).Text)
If status = 1 Then
e.Row.Cells(1).Text = "Active"
e.Row.Cells(1).BackColor = Color.Green
Else
e.Row.Cells(1).Text = "Inactive"
e.Row.Cells(1).BackColor = Color.Red
End If
End If
End Sub
Screenshot