Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a table(PayRoll_AttendanceStatus) that contains 4 columns like
======================================
StatusID | Status | ShortName | color
======================================
   01    | Present|     P     |  Yellow
   02    | Absent |     A     |  Red
=======================================

from above mentioned table I used to mark day wise attendance into another table (PayRollMarkAttendance)and it contains values like
====================================================
MarkId  |  Employee_Code  |  StatusID  |  DateTime
====================================================
1       |        001      |    01      |  2016-06-11
2       |        002      |    02      |  2016-06-11
====================================================

For view I am using Pivot table and the code for pivot table is
SQL
SELECT * FROM (SELECT [EMPLOYEENAME] ,[EMPLOYEE_CODE]  ,[ShortName], DATENAME(M, [DateTime])as [Month]  ,DAY([DateTime]) as [DayValue] FROM [View_PayRollMarkAttendance]) as Composite PIVOT (MAX([ShortName]) FOR [DayValue] IN ([1], [2], [3], [4],[5], [6], [7], [8], [9],[10], [11], [12], [13], [14],[15], [16], [17], [18], [19],[20], [21], [22], [23], [24],[25], [26], [27], [28], [29],[30],[31])) AS PivotTable

Through this pivottable I can view my status quite good. But I want to view ABSENT AND PRESENT through its unique code color. Absent for Red and Present for Yellow.

Please help any one.

Thanks
Posted
Updated 10-Jun-16 19:01pm
v2
Comments
BillWoodruff 10-Jun-16 17:11pm    
What Control or user interface are you using to actually render the color so they are visible ?
Member 12245539 10-Jun-16 17:21pm    
Windows Form C# and Gridview
Dave Kreskowiak 10-Jun-16 18:32pm    
One more time. WHICH UI TECHNOLOGY ARE YOU USING? Is this a Windows Forms app, WPF, ASP.NET, Console, ?? WHAT? We have no idea what controls you're using to display this information so it's impossible to tell you how to color it!
'
Member 12245539 11-Jun-16 0:21am    
Windows Form with C#
Dave Kreskowiak 11-Jun-16 0:25am    
AND WHICH CONTROL(S) ARE YOU USING TO SHOW THIS DATA?? A DataGridView, a bunch of TextBoxes, ... what??

Come on. We can't see your screen or read your mind.

1 solution

updated solution:

C#
private void Form1_Load(object sender, EventArgs e)
      {
          DataTable dtStatusColor = new DataTable();
          dtStatusColor.Columns.Add("ShortName");
          dtStatusColor.Columns.Add("color");
          dtStatusColor.Rows.Add("P", "Yellow");
          dtStatusColor.Rows.Add("A", "Red");
          Dictionary<string, string> dictColors = new Dictionary<string, string>();
          foreach (DataRow row in dtStatusColor.Rows)
              dictColors.Add(row["ShortName"].ToString(), row["color"].ToString());



          DataTable dtStatus = new DataTable();
          dtStatus.Columns.Add("EMPLOYEENAME");
          dtStatus.Columns.Add("EMPLOYEE_CODE");
          dtStatus.Columns.Add("ShortName");
          dtStatus.Columns.Add("Month");
          dtStatus.Columns.Add("DayValue");
          dtStatus.Rows.Add("karthik", "E01", "P", "Jan", 25);
          dtStatus.Rows.Add("parthip", "E02", "P", "Feb", 25);
          dtStatus.Rows.Add("kavya", "E03", "A", "Mar", 25);



          dataGridView1.DataSource = dtStatus;
          int columnIndex = 2; // index value for the status column ( zero based index )
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
              if (row.Index > -1 && row.Index < dataGridView1.Rows.Count - 1)
              {
                  string status = row.Cells[columnIndex].Value.ToString();
                  row.Cells[columnIndex].Style.BackColor = System.Drawing.Color.FromName(dictColors[status]);

              }
          }

      }
 
Share this answer
 
v2
Comments
Member 12245539 11-Jun-16 1:25am    
Thanks Mr Karthik,
But there is little bit issue.
The color I want to pick from my First table (PayRoll_AttendanceStatus) that I have mentioned above. I you see, there is one column for color only. I want pick from color column only. Please see.
Karthik_Mahalingam 11-Jun-16 1:26am    
ok, does the datatable has the color value ?

what is the sample output of the table to be displayed in the grid ?
Member 12245539 11-Jun-16 1:43am    
Yes it has color value.

the code that I am using is
try
{
string Querry = "SELECT * FROM(SELECT [EMPLOYEENAME] ,[EMPLOYEE_CODE] ,[ShortName] ,DATENAME(M, [DateTime])as [Month] ,DAY([DateTime]) as [DayValue] FROM [View_PayRollMarkAttendance]) as Composite PIVOT (MAX([ShortName]) FOR [DayValue] IN ([1], [2], [3], [4],[5], [6], [7], [8], [9],[10], [11], [12], [13], [14],[15], [16], [17], [18], [19],[20], [21], [22], [23], [24],[25], [26], [27], [28], [29],[30],[31])) AS PivotTable";

SqlDataAdapter sda = new SqlDataAdapter(Querry, con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataGridView.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Karthik_Mahalingam 11-Jun-16 1:58am    
so you need to get the color from status table.
Karthik_Mahalingam 11-Jun-16 2:09am    
check the updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900