Hi George616,
Refer below sample code.
HTML
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="OnRowDataBound"
class="table table-bordered table-condensed table-hover" Width="100%">
<EmptyDataTemplate>
<div style="text-align: center; font-weight: bolder; font-size: medium;">
<asp:Label ID="labelTemp" runat="server" Text="No Activity Recorded"></asp:Label>
</div>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Width="30px" Height="30px" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" HeaderText="Action" />
</Columns>
</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 (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT Email,UserRole,Name,CreateDate,Image FROM Users", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
if (dr["UserRole"].ToString().ToLower() == "a")
{
(e.Row.Cells[1].Controls[0] as Button).Visible = true;
}
else if (dr["UserRole"].ToString().ToLower() == "u")
{
(e.Row.Cells[1].Controls[0] as Button).Visible = false;
}
if (dr.Row.Table.Columns.Contains("image"))
{
if (dr["image"] != DBNull.Value)
{
string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["image"]);
(e.Row.FindControl("Image1") as Image).ImageUrl = imageUrl;
}
}
}
GridViewRow row = e.Row;
List<TableCell> cells = new List<TableCell>();
foreach (DataControlField column in GridView1.Columns)
{
TableCell cell = row.Cells[0];
row.Cells.Remove(cell);
cells.Add(cell);
}
row.Cells.AddRange(cells.ToArray());
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT Email,UserRole,Name,CreateDate,Image FROM Users", conn)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
If dr("UserRole").ToString().ToLower() = "a" Then
TryCast(e.Row.Cells(1).Controls(0), Button).Visible = True
ElseIf dr("UserRole").ToString().ToLower() = "u" Then
TryCast(e.Row.Cells(1).Controls(0), Button).Visible = False
End If
If dr.Row.Table.Columns.Contains("image") Then
If dr("image") <> DBNull.Value Then
Dim imageUrl As String = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("image"), Byte()))
TryCast(e.Row.FindControl("Image1"), Image).ImageUrl = imageUrl
End If
End If
End If
Dim row As GridViewRow = e.Row
Dim cells As List(Of TableCell) = New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
Dim cell As TableCell = row.Cells(0)
row.Cells.Remove(cell)
cells.Add(cell)
Next
row.Cells.AddRange(cells.ToArray())
End Sub
Screenshot
![](https://i.imgur.com/1YEeSic.jpg)