Hi indradeo,
Ues the GridView OnRowDataBound event handler and loop through each cell in the row. Then replace the Invalid value with blank in each cell.
Refer below example.
HTML
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false"
OnDataBound="OnDataBound" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Parameters" HeaderText="Parameters" />
<asp:BoundField DataField="PM10" HeaderText="PM10" />
<asp:BoundField DataField="PM2.5" HeaderText="PM2.5" />
<asp:BoundField DataField="SO2" HeaderText="SO2" />
<asp:BoundField DataField="NO" HeaderText="NO" />
<asp:BoundField DataField="NO2" HeaderText="NO2" />
<asp:BoundField DataField="NOx" HeaderText="NOx" />
<asp:BoundField DataField="CO2" HeaderText="CO2" />
</Columns>
</asp:GridView>
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)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Parameters", typeof(string)),
new DataColumn("PM10", typeof(string)),
new DataColumn("PM2.5",typeof(string)),
new DataColumn("SO2", typeof(string)),
new DataColumn("NO", typeof(string)),
new DataColumn("NO2", typeof(string)),
new DataColumn("NOx", typeof(string)),
new DataColumn("CO2", typeof(string))
});
dt.Rows.Add("2023-12-28 15:00:00+00:00", "9.26", "3.79", "Invalid", "20.67", "Invalid", "42.58", "Invalid");
dt.Rows.Add("AVG DATA", "9.33", "Invalid", "9.26", "9.26", "21.43", "42.01", "20.67");
dt.Rows.Add("MAX DATA", "Invalid", "3.90", "Invalid", "Invalid", "19.93", "44.30", "Invalid");
dt.Rows.Add("MIN DATA", "9.26", "Invalid", "3.90", "9.45", "Invalid", "39.80", "Invalid");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnDataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableHeaderCell cell = new TableHeaderCell();
cell.Text = "UoM";
cell.ColumnSpan = 1;
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Text.ToLower()== "invalid")
{
cell.Text = string.Empty;
}
}
}
}
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() {
New DataColumn("Parameters", GetType(String)),
New DataColumn("PM10", GetType(String)),
New DataColumn("PM2.5", GetType(String)),
New DataColumn("SO2", GetType(String)),
New DataColumn("NO", GetType(String)),
New DataColumn("NO2", GetType(String)),
New DataColumn("NOx", GetType(String)),
New DataColumn("CO2", GetType(String))})
dt.Rows.Add("2023-12-28 15:00:00+00:00", "9.26", "3.79", "Invalid", "20.67", "Invalid", "42.58", "Invalid")
dt.Rows.Add("AVG DATA", "9.33", "Invalid", "9.26", "9.26", "21.43", "42.01", "20.67")
dt.Rows.Add("MAX DATA", "Invalid", "3.90", "Invalid", "Invalid", "19.93", "44.30", "Invalid")
dt.Rows.Add("MIN DATA", "9.26", "Invalid", "3.90", "9.45", "Invalid", "39.80", "Invalid")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
Dim cell As TableHeaderCell = New TableHeaderCell()
cell.Text = "UoM"
cell.ColumnSpan = 1
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 1
cell.Text = "µg/m3"
row.Controls.Add(cell)
row.BackColor = ColorTranslator.FromHtml("#3AC0F2")
GridView1.HeaderRow.Parent.Controls.AddAt(0, row)
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For Each cell As TableCell In e.Row.Cells
If cell.Text.ToLower() = "invalid" Then
cell.Text = String.Empty
End If
Next
End If
End Sub
Screenshot