我想在datagridview标题中增加日期 [英] I wanna a increase date in my datagridview header
问题描述
我正在研究考勤软件我正面临一个问题,即我想在每个标题上增加日期,你可以在我的datagridview https:/ /imgur.com/a/hP0Yedo [ ^ ]
实际上,我想显示整个月的出勤率,我想从datetimepicker获取日期和日期,伙计们,请帮我解决一下或者告诉我该怎么做,谢谢,这是我的代码
我尝试过的方法:
I am working on attendance software I am facing an issue that is I want to increase date on every header here you can my datagridview https://imgur.com/a/hP0Yedo[^]
Actually, I wanna show whole month attendance and I wanna get dates and days from datetimepicker please, guys, help me give me the solution or tell me how can I do this, please Thanks Here is my code
What I have tried:
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);
}
}
推荐答案
我建议使用 DataTable [ ^ ]对象为DataSource [ ^ ] ofdataGridView4
,因为这个组件使您能够以简单的方式管理数据:
I'd suggest to use DataTable[^] object as a DataSource[^] ofdataGridView4
, because this component enables you to manage data in easy way:
DataTable dt = new DataTable();
//get initial date from DateTimePicker
//i'm using hard-coded date, because i'm lazy to create graphical interface
DateTime startdate = new DateTime(2019,1,1);
DateTime enddate = startdate.AddMonths(1).AddDays(-1);
TimeSpan ts = (TimeSpan)(enddate-startdate);
int dayscount = (int)ts.TotalDays;
CultureInfo ci = new CultureInfo("en-US");
for(int i=0; i<dayscount; i++)
{
dt.Columns.Add(new DataColumn(startdate.AddDays(i).ToString("yyyy-MM-dd"), typeof(string)));
}
//check:
//dt.Columns.Cast<DataColumn>().Select(c=>c.ColumnName).Dump();
//now - use your logic to add data
//
//...
//
//finally - bind data with datatable
dataGridView4.AutoGenerateColumns = true;
dataGridView4.DataSource = dt;
使用上面的代码,你会得到这样的列:
Using above code, you'll get columns like this:
2019-01-01 2019-01-02 ... 2019-01-30 2019-01-31
详情请见:
如何:将数据绑定到Windows窗体DataGridView控件Microsoft Docs [ ^ ]
Object.ToString方法(系统)| Microsoft Docs [ ^ ]
这篇关于我想在datagridview标题中增加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!