Change dynamically generated ASP.Net GridView cell Text Color using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

I want to change the text colour from the grid view that is displayed in the panel. When the text is greater than 0, the text colour will be displayed with the green an when the text is less than 0 the text colur will be red and not to displayed minus (-) sign.

When the gird view display the panel looks like this I want to change them as follows

Consumer Name Buying Count No
James 12
Marry -31

12 must be displayed with the green and the 31 must be displayed red without minus.

Consumer Name Buying Count No
James 12
Marry 31

How can I create the girdview inside the panel?

private void Bind_GridView()
{
    int j = 0; 
    DataSet ds = (Session["CalculateInfo_DS"] == null) ? null : (DataSet)Session["CalculateInfo_DS"];
    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.DataSource = ds.Tables[i];
            objGV.DataBind();
            PanelResult.Controls.Add(objGV);
        }
    }
}

html

<asp:panel id="PanelResult" runat="server">
</asp:panel>

 

Posted 5 months ago

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