Hi ramco1917,
Please refer below sample code.
HTML
<asp:Literal ID="ltTable" runat="server" />
CSS
.primary { background-color: dodgerblue; }
.danger { background-color: red; }
Code
CS
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Status") });
dt.Rows.Add(1, "John Hammond", "Active");
dt.Rows.Add(2, "Mudassar Khan", "Inactive");
dt.Rows.Add(3, "Suzanne Mathews", "Active");
dt.Rows.Add(4, "Robert Schidner", "Inactive");
StringBuilder sb = new StringBuilder();
//Table start.
sb.Append("<table>");
//Adding HeaderRow.
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th>" + column.ColumnName + "</th>");
}
sb.Append("</tr>");
//Adding DataRow.
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
if (column.ColumnName == "Status")
{
if (row[column.ColumnName].ToString().ToLower() == "active")
{
sb.Append("<td class='primary'>" + row[column.ColumnName].ToString() + "</td>");
}
else
{
sb.Append("<td class='danger'>" + row[column.ColumnName].ToString() + "</td>");
}
}
else
{
sb.Append("<td>" + row[column.ColumnName].ToString() + "</td>");
}
}
sb.Append("</tr>");
}
//Table end.
sb.Append("</table>");
ltTable.Text = sb.ToString();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Status")})
dt.Rows.Add(1, "John Hammond", "Active")
dt.Rows.Add(2, "Mudassar Khan", "Inactive")
dt.Rows.Add(3, "Suzanne Mathews", "Active")
dt.Rows.Add(4, "Robert Schidner", "Inactive")
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<table>")
sb.Append("<tr>")
For Each column As DataColumn In dt.Columns
sb.Append("<th>" & column.ColumnName & "</th>")
Next
sb.Append("</tr>")
For Each row As DataRow In dt.Rows
sb.Append("<tr>")
For Each column As DataColumn In dt.Columns
If column.ColumnName = "Status" Then
If row(column.ColumnName).ToString().ToLower() = "active" Then
sb.Append("<td class='primary'>" & row(column.ColumnName).ToString() & "</td>")
Else
sb.Append("<td class='danger'>" & row(column.ColumnName).ToString() & "</td>")
End If
Else
sb.Append("<td>" & row(column.ColumnName).ToString() & "</td>")
End If
Next
sb.Append("</tr>")
Next
sb.Append("</table>")
ltTable.Text = sb.ToString()
End If
End Sub
Screenshot