-1

I am working on attendance software I am facing an issue that is I want to increase date on every header.

Here you can see my datagridview : https://i.stack.imgur.com/cXm2u.jpg

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:

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);
            }
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
digisoft
  • 15
  • 7
  • How big the gap can be between two dates, can the years vary as well like 2-11-18 to 2-4-19 – Anil Feb 26 '19 at 02:56
  • Without knowing the full context of what you are wanting to accomplish… It appears “odd” to approach this as you describe. First, it appears that there would never be more than ONE (1) row? In addition, if there are thirty (30) days in the month, and there are three (3) columns for each day (in-out-total) … then there would be ONE row with 30x3 = “90” columns? This seems odd. – JohnG Feb 26 '19 at 06:49
  • Have you considered having each “row” in the grid represent a single “day” date by adding a “date” column? I recommend you look up some of the `DateTime` properties and methods… you should find methods that will add a “day” to a date like… `dt = dt.AddDays(1);`. There are a few methods as this and I am confident they will help when adding/subtracting dates. Bear in mind the `DateTime` objects are immutable, therefore, `dt.AddDays(1)` RETURNS a new `DateTime` object with the added day and does NOT change `dt`. – JohnG Feb 26 '19 at 06:49
  • the error was not resolved by dt = DT.AddDays(1); it;s increase date just for one time not cont9inuously please see my code and please if you dont mind so please correct my code – digisoft Feb 26 '19 at 13:37

1 Answers1

0

I solved it myself, here is the solution:

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);
            }
        }
Vickel
  • 7,879
  • 6
  • 35
  • 56
digisoft
  • 15
  • 7