Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all....

I doing Emp_attendance works in C#(Windows Application), in that grid view i am using many column colors in depend on condition. the following grid sample is simplified my question...

VB
-----------------------------------------------------
empname   |  1   | 2   | 3 | 4 | 5 |.....|11|...|30or 31(in depend on month)
----------------------------------------------------
  xxx     |  P   | P   | P |   | P |.....|  |.....
  ddd     |  P   | P   | A |   | A |.....|  |.....
  rrr     |  A   | A   | P |   | A |.....|  |.....
----------------------------------------------------



1)In this grid view the Column Header 1 to 31 is days in month
2)P=Present , A=Absent.
3)See the Grid view column 4 & 11 that column Back color is gray. Because 4th & 11th is Sunday so the employee attendance is not fill.
4)This case is not take the gridview column index because in this gridview column count is changed depend on month(Ex--Jan(31)Feb(28or29)Apr(30)), So the column index is not taking for this case...

I want the mouse enter that particular 4th & 11th column the tooltip is display the message "The Day is Sunday"

i try to many way but not use, my try code is below...
C#
private void GridView_MouseHover(object sender, EventArgs e)
        {
           // Color gridcolor = GridView.CurrentCell.Style.BackColor;
          //  GridView.CurrentCell.Style.BackColor = Color.Gray;
           // GridView.DefaultCellStyle.BackColor = Color.Gray;
         //  MessageBox.Show(gridcolor.ToString());
               if (GridView.CurrentCell.Style.BackColor == Color.Gray)
           {
               GridView.CurrentCell.ToolTipText = "Sunday";
           }
        }



please help me...
Posted
Updated 12-Dec-13 5:38am
v2
Comments
AnthonyMG 12-Dec-13 7:41am    
Gray color is set for Grid Cell's Forecolor or BackColor ?
sv sathish 12-Dec-13 11:03am    
Backcolor
Web app or Windows app?
sv sathish 12-Dec-13 11:03am    
windows apps

You need not check for color , if your sure that your column will remain constant (4th Column)
then get the column name and display tooltip whenever mouse hover on this column..
C#
private void GridView_MouseHover(object sender, EventArgs e)
{
   // columnname of the grid
   String columnName = GridView.HeaderRow.Cells[i].Text;
   
   // check for your desired columnname 
   if(columnName == yourdesiredcolumnName)
   {         
        GridView.CurrentCell.ToolTipText = "Sunday";
   }  
}


1.I know your column index will change often, but your column name will not change rite.
so you can get the column name from the Gridcontrol and display tooltip only for this column

let me know if have any issue.
 
Share this answer
 
v3
Comments
sv sathish 12-Dec-13 11:20am    
Sir i could not understand your answer pls clarified your answer...
AnthonyMG 13-Dec-13 1:50am    
Sathish, I have updated the solution more clearly , please check it.
It's just simple
You are using this code
private void GridView_MouseHover(object sender, EventArgs e)
        {
           // Color gridcolor = GridView.CurrentCell.Style.BackColor;
          //  GridView.CurrentCell.Style.BackColor = Color.Gray;
           // GridView.DefaultCellStyle.BackColor = Color.Gray;
         //  MessageBox.Show(gridcolor.ToString());
               if (GridView.CurrentCell.Style.BackColor == Color.Gray)
           {
               GridView.CurrentCell.ToolTipText = "Sunday";
           }
        }


but [current cell] is the cell which you select not hover over
Instead use this:-

private void GridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
       {
           try
           {
               if (GridView.CurrentRow.Index > -1 && GridView.CurrentCell.Style.BackColor == Color.Gray)
               {
                   GridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "Sunday";
               }
           }
           catch (Exception Ex) {  }
       }


Please click accept solution if your query has been solved.
 
Share this answer
 
v2
if you use DataGridView (WinForms), you can solve the problem easier: DataGridView class has event CellToolTipTextNeeded
handle this event to set a tool tip
C#
private void grid_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            int year = 2013;
            int month = 12; 
            var dt = new DateTime(year, month, e.ColumnIndex);
            if (dt.DayOfWeek == DayOfWeek.Saturday)
               e.ToolTipText = "Sunday";
        }
 
Share this answer
 
v2
Comments
sv sathish 12-Dec-13 11:11am    
sir the gridview column index is not permanent,because month wise the day is changing so the gridview column index also changed..
Alexander Sharykin 13-Dec-13 3:14am    
this is definitely a bad idea to rely on any view properties (such as cell color) in your business logic
though "column index is not permanent", it's possible to know day of week.
variant of solution
int year = 2013;
int month = 12;
var dt = new DateTime(year, month, e.ColumnIndex);
if (dt.DayOfWeek == DayOfWeek.Saturday)
e.ToolTipText = "Sunday";

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