根据Datagridview中单元格的值更改Datagridview中的行颜色 [英] Change Row Color in Datagridview based on values of a cells in another Datagridview

查看:418
本文介绍了根据Datagridview中单元格的值更改Datagridview中的行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#winform sql server



在我的数据库中,我尝试管理我小公司的员工假期,所以我创建了两个表



第一个表称为 tblMember



idMember (PK,int,not null)



memberName (varchar(50) null)



该表与一对多的关系相关



第二个表称为 tblVacations



idVacation (PK,int,not null)



vacationStart (date,null)



(FK,int,null)



idMember_L / p>

然后我创建了一个带有两个datagridview的窗体,首先调用 dg_member 填充其数据(来自tblMember的名称)throw a stored程序



第二个datagridview dgVacation 填充相关数据



<现在我想要colo红色的红色名字在 dg_member 今天缺席(今天在 vacationStart vacationEnd之间),如果会员今天来(今天等于 vacationEnd ),名称变为绿色



我尝试了代码:

  private void dgMember_Grade_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
foreach(DataGridViewRow Myrow in dgMember_Grade.Rows)
{
var cvStart = dgVacation.Rows [0] .Cells [1] .Value;
var cvEnd = dgVacation.Rows [0] .Cells [2] .Value;

if(cvStart == null || cvStart == DBNull.Value)
continue;

DateTime startDate = Convert.ToDateTime(cvStart);
DateTime endDate = Convert.ToDateTime(cvEnd);

if(startDate< DateTime.Now&& endDate> DateTime.Now)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else if(endDate == DateTime.Now)
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
}

但它给我一个错误



索引超出范围。必须是非负数而小于收藏的大小。





谢谢

解决方案

  declare @tmw Date = DATEADD(day, 1,getdate())
选择


m.MemberName
,当t.idMember_L为空时的情况,那么'False'否则'True'结束为OnHoliday
,case(select count(tm.idMember_L)from tblVacations tm where
m.idMember = tm.idMember_L and
t.vacationStart< = @tmw and t.vacationEnd> = @ tmw)> 0
然后'False'else'True'结束为OnHolidayTomorrow
from tblMember m
left outer join tblVacations t
on m.idMember = t.idMember_L and
t .vacationStart< = getdate()和t.vacationEnd> = getdate()


I'm using c# winform sql server

In my database I try to manage the employee vacations in my small company so I was created two tables

first table called tblMember

idMember (PK, int, not null)

memberName (varchar(50), null)

that table related in one to many relationship to

second table called tblVacations

idVacation (PK, int, not null)

vacationStart (date, null)

vacationEnd (date, null)

idMember_L(FK, int, null)

then I created a form with two datagridview the first called dg_member populate its data (names from tblMember) throw a stored procedure

the second datagridview dgVacation populate the related data

now I want to colored with red the name in dg_member who absent today ( today is fall between vacationStart and vacationEnd ) and if the member is coming today ( today equal vacationEnd ) the name turn to green

i tried the code :

private void dgMember_Grade_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            foreach (DataGridViewRow Myrow in dgMember_Grade.Rows)
            {
                var cvStart = dgVacation.Rows[0].Cells[1].Value;
                var cvEnd = dgVacation.Rows[0].Cells[2].Value;

                if (cvStart == null || cvStart == DBNull.Value)
                    continue;

                DateTime startDate = Convert.ToDateTime(cvStart);
                DateTime endDate = Convert.ToDateTime(cvEnd);

                if (startDate < DateTime.Now && endDate > DateTime.Now)
                {
                    Myrow.DefaultCellStyle.BackColor = Color.Red;
                }
                else if (endDate == DateTime.Now)
                {
                    Myrow.DefaultCellStyle.BackColor = Color.Green;
                }
            }
        }

but it gives me an error

Index was out of range. Must be non-negative and less than the size of the collection.

thank you

解决方案

declare @tmw Date  = DATEADD(day, 1,getdate())
select


m.MemberName
, case when t.idMember_L is null then 'False' else 'True' end as OnHoliday
, case when (select count (tm.idMember_L) from tblVacations tm where
    m.idMember = tm.idMember_L and
     t.vacationStart <= @tmw and t.vacationEnd >= @tmw ) > 0
     then 'False' else 'True' end as OnHolidayTomorrow
 from tblMember m
     left outer join tblVacations t
     on m.idMember = t.idMember_L and
     t.vacationStart <= getdate() and t.vacationEnd >= getdate()

这篇关于根据Datagridview中单元格的值更改Datagridview中的行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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