How to Change Cell and Text color Based on specific Text using dynamic Pivot Table
tblAttendance
AdmissionNo ADate Remarks
R-001 15.01.19 Present
R-002 15.01.19 Leave
R-003 15.01.19 Present
R-001 16.01.19 Absent
R-002 16.01.19 Present
R-003 16.01.19 Leave
Requirement is that
The cell background should be yellow for leave
The cell background should be red for absent
The cell background should be blue for present
<asp:GridView ID="GridView1" runat="server" AllowPaging="false"
Class="table table-striped table-bordered table-hover" RowStyle-Wrap="false"
HeaderStyle-Wrap="false" onrowdatabound="GridView1_RowDataBound" >
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
con = new SqlDbConnect();
con.SqlQuery(@"SELECT DISTINCT ADate INTO #Dates
FROM tblAttendance
ORDER BY ADate
DECLARE @cols NVARCHAR(4000)
SELECT @cols = COALESCE(@cols + ',[' + CONVERT(varchar, ADate, 106) + ']','[' + CONVERT(varchar, ADate, 106) + ']') FROM #Dates ORDER BY ADate
DECLARE @qry NVARCHAR(4000)
SET @qry =
'SELECT * FROM
(SELECT fc.AdmissionNo,SName,FName,AcademicName,ClassName,SectionName,FPhone,Remarks , ADate
FROM tblAttendance
as fc inner join tblStdReg as sr on fc.AdmissionNo=sr.AdmissionNo and Active_Status=''Active''
inner join tblAcademicYear as ay on sr.YearID=ay.YearID inner join tblDefClass as dc on sr.ClassID=dc.ClassID
inner join tblDefSection as dsb on sr.SectionID=dsb.SectionID
)std
PIVOT (MAX(Remarks) FOR ADate IN (' + @cols + ')) AS stat'
EXEC(@qry)
DROP TABLE #Dates");
adapt.SelectCommand = con.Cmd;
adapt.Fill(sTable);
if (sTable.Rows.Count > 0)
{
GridView1.Visible = true;
GridView1.DataSource = sTable;
GridView1.DataBind();
lblTotal.Text = GridView1.Rows.Count.ToString();
}
else
{
GridView1.Visible = false;
Response.Write("No Record Found");
lblTotal.Text = "0";
return;
}
con.conClose();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < sTable.Rows.Count; i++)
{
for (int j = 0; j < sTable.Columns.Count; j++)
{
if (sTable.Rows[i][j].ToString().Equals("Absent"))
{
}
}
}
}