如何在C#中获取网格颜色 [英] How to Get Grid Color in C#

查看:81
本文介绍了如何在C#中获取网格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好....



我做的Emp_attendance适用于C#(Windows应用程序),在该网格视图中我使用多种列颜色取决于条件。以下网格示例简化了我的问题...



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...

-----------------------------------------------------
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)在此网格视图中列标题1到31是每月的天数

2)P =存在,A =缺席。

3)参见网格视图第4栏和第4栏。 11该列背面颜色为灰色。因为4& 11日是星期日所以员工出勤没有填写。

4)这个案例不采取gridview列索引,因为在这个gridview列中计数根据月份而变化(Ex - Jan(31)Feb (28或29)4月(30)),所以列索引不适用于这种情况...



我希望鼠标输入特定的第4和第4个第11列工具提示显示消息The Day is Sunday



i尝试多种方式但不使用,我的尝试代码如下...




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...

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...

推荐答案

如果确定列保持不变(第4列),则无需检查颜色

然后获取列名称并显示工具提示只要鼠标悬停在此列上..

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..
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.我知道您的列索引会经常更改,但您的列名不会改变仪式。

这样你就可以从Gridcontrol中获取列名,只显示该列的工具提示



让我知道是否有任何问题。



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.


这很简单

您正在使用此代码

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.


如果您使用DataGridView(WinForms),您可以更轻松地解决问题:DataGridView类有事件CellToolTipTextNeeded

句柄此事件设置工具提示

if you use DataGridView (WinForms), you can solve the problem easier: DataGridView class has event CellToolTipTextNeeded
handle this event to set a tool tip
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";
        }


这篇关于如何在C#中获取网格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆