我想在datagridview标题中增加日期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想在datagridview标题中增加日期相关的知识,希望对你有一定的参考价值。

我正在研究考勤软件我面临的一个问题是我想在每个标题上增加日期。

在这里你可以看到我的datagridview:https://imgur.com/a/hP0Yedo

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

拜托,伙计们,请帮我解决一下,或者告诉我怎么办呢

谢谢

这是我的代码:

int nnnnn2 = 0;

        private void dataGridView4_Paint(object sender, PaintEventArgs e)
        {
            nnnnn2 = DateTime.DaysInMonth(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month);
            string[] weeks = new string[nnnnn2];
            string[] date = new string[nnnnn2];

            for (int i = 0; i < nnnnn2;i++)
            {
                newvar = dateTimePicker2.Text;
                //newvar = (newvar.ToString().Substring(0, newvar.ToString().Length - 11));
                DateTime dt = DateTime.Parse(newvar);
                weeks[i] = dt.DayOfWeek.ToString();
                date[i] = newvar;
            }

            for (int j = 0; j < (weeks.Count()); 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(date[j / 3] + "
" + weeks[j / 3],
                this.dataGridView4.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView4.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                    format);
            }
        }
答案

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

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]+"
"+weeks[j / 3],
                this.dataGridView4.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView4.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                    format);
            }
        }

以上是关于我想在datagridview标题中增加日期的主要内容,如果未能解决你的问题,请参考以下文章

C#Winform中DataGridView往某一行新增数据

如何使用ComboBox过滤datagridview

如何在按钮单击时更新datagridview中的记录?

我想在 javascript 中计算保修到期日期

如何在 C# 中解析复杂的 JSON?最终我想在 DataGridView 中显示结果 [关闭]

关于WinForm下DataGridView控件行的选择的问题