Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

C#
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);
            }
        }
Posted
Updated 25-Feb-19 20:27pm
v2

1 solution

I'd suggest to use DataTable[^] object as a DataSource[^] of dataGridView4, because this component enables you to manage data in easy way:

C#
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 


For further details, please see:
How to: Bind data to the Windows Forms DataGridView control | Microsoft Docs[^]
Object.ToString Method (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900