我想在我的datagridview标头中增加日期

时间:2019-02-26 01:14:48

标签: c# datagridview header datetimepicker time-and-attendance

我正在使用考勤软件,但是遇到一个问题,我想增加每个标题的日期。

在这里您可以看到我的datagridview:Entity Framework with Nuget from Microsoft.

实际上,我想显示整个月的出勤率,也想从datetimepicker获取日期和日期。

请,伙计们,请帮助我给我解决方案,或者告诉我我该怎么做

谢谢

这是我的代码:

pf = ∑

1 个答案:

答案 0 :(得分:0)

我自己解决了,这是解决方法:

private void dataGridView4_Paint(object sender, PaintEventArgs e)
        {
            var startDate = dateTimePicker2.Value.Date;
            var endDate = dateTimePicker1.Value.Date;
            List<string> weeks = new List<string>();
            List<string> dates = new List<string>();
            while (startDate <= endDate)
            {
                weeks.Add(startDate.DayOfWeek.ToString());
                dates.Add(startDate.Date.ToString());
                startDate = startDate.AddDays(1);

            }
            for (int j = 0; j < (weeks.Count() * 3); j += 3)
            {
                Rectangle r1 = this.dataGridView4.GetCellDisplayRectangle(j, -1, true);
                int w2 = this.dataGridView4.GetCellDisplayRectangle(j + 1, -1, true).Width;
                r1.X += 1;
                r1.Y += 1;
                r1.Width = r1.Width * 3 - 2;
                r1.Height = r1.Height / 2 - 2;
                e.Graphics.FillRectangle(new SolidBrush(this.dataGridView4.ColumnHeadersDefaultCellStyle.BackColor), r1);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                e.Graphics.DrawString(dates[j / 3]+"\n"+weeks[j / 3],
                this.dataGridView4.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView4.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                    format);
            }
        }