我正在研究出勤软件,我面临的问题是我想增加每个标题的日期。
在这里您可以看到我的datagridview:https://i.stack.imgur.com/cxm2u.jpg
实际上,我想展示整个月的出勤率,我想从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] + "n" + 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]+"n"+weeks[j / 3],
this.dataGridView4.ColumnHeadersDefaultCellStyle.Font,
new SolidBrush(this.dataGridView4.ColumnHeadersDefaultCellStyle.ForeColor),
r1,
format);
}
}