Hey SamMyat,
Please refer below sample.
HTNL
<div>
<asp:Panel ID="PanelResult" runat="server">
</asp:Panel>
</div>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Bind_GridView();
}
}
private void Bind_GridView()
{
DataSet ds = new DataSet();
int j = 0;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Consumer Name", typeof(string)), new DataColumn("Buying Count No", typeof(int)) });
dt.Rows.Add("James", 12);
dt.Rows.Add("Marry", -13);
ds.Tables.Add(dt);
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Consumer Name", typeof(string)), new DataColumn("Buying Count No", typeof(int)) });
dt.Rows.Add("James", -12);
dt.Rows.Add("Marry", 13);
ds.Tables.Add(dt);
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables.Count; i++)
{
j = i + 1;
GridView objGV = new GridView();
objGV.ID = "ResultDG" + j;
objGV.AutoGenerateColumns = true;
objGV.RowDataBound += new GridViewRowEventHandler(objGV_RowDataBound);
objGV.DataSource = ds.Tables[i];
objGV.DataBind();
PanelResult.Controls.Add(objGV);
}
}
}
void objGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(e.Row.Cells[1].Text);
if (id > 0)
{
e.Row.Cells[1].ForeColor = Color.Green;
}
else
{
e.Row.Cells[1].Text = Math.Abs(id).ToString();
e.Row.Cells[1].ForeColor = Color.Red;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Bind_GridView()
End If
End Sub
Private Sub Bind_GridView()
Dim ds As DataSet = New DataSet()
Dim j As Integer = 0
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Consumer Name", GetType(String)), New DataColumn("Buying Count No", GetType(Integer))})
dt.Rows.Add("James", 12)
dt.Rows.Add("Marry", -13)
ds.Tables.Add(dt)
dt = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Consumer Name", GetType(String)), New DataColumn("Buying Count No", GetType(Integer))})
dt.Rows.Add("James", -12)
dt.Rows.Add("Marry", 13)
ds.Tables.Add(dt)
If ds.Tables.Count > 0 Then
For i As Integer = 0 To ds.Tables.Count - 1
j = i + 1
Dim objGV As GridView = New GridView()
objGV.ID = "ResultDG" & j
objGV.AutoGenerateColumns = True
AddHandler objGV.RowDataBound, AddressOf objGV_RowDataBound
objGV.DataSource = ds.Tables(i)
objGV.DataBind()
PanelResult.Controls.Add(objGV)
Next
End If
End Sub
Private Sub objGV_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
If id > 0 Then
e.Row.Cells(1).ForeColor = Color.Green
Else
e.Row.Cells(1).Text = Math.Abs(id).ToString()
e.Row.Cells(1).ForeColor = Color.Red
End If
End If
End Sub
Screenshot
