Make calendar in datagridview c#

Anonymous
2024-06-08T10:06:27.07+00:00

Good day!

Please take a look the image below.

1

I want to input day from June to July but no color displayed from start to end date.

And here my code.

int offset = 4;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        if (row.Cells.Count >= 4)
                        {
                            DateTime startBook;
                            DateTime endBook;
                            if (DateTime.TryParse(row.Cells[2].Value?.ToString(), out startBook) &&
                                DateTime.TryParse(row.Cells[3].Value?.ToString(), out endBook))
                            {
                                int startBook_C = startBook.Day;
                                int endBook_C = endBook.Day;
                                int TotalDays = endBook_C - startBook_C;
                                for (int a = 0; a <= TotalDays; a++)
                                {
                                    dataGridView1.Rows[row.Index].Cells[offset + startBook_C - 1 + a].Style.BackColor = Color.Blue;
                                }
                            }
                        }
                    }
                }


Thank you very much to those can fix my problem.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. KOZ6.0 6,735 Reputation points
    2024-06-09T04:44:38.54+00:00

    Previous question: https://learn.microsoft.com/en-us/answers/questions/1658584/calendar-booking-using-datagridview

    View_CellPainting method from my answer can be used as is by defining it as follows:

    public partial class Form1 : Form
    {
        private DataGridView view { get => dataGridView1; }
        private DataGridViewColumn startTimeColumn { get => view.Columns[2]; }
        private DataGridViewColumn endTimeColumn { get => view.Columns[3]; }
        public int gridYear { get => monthCalendar1.SelectionStart.Year; }
        public int gridMonth { get => monthCalendar1.SelectionStart.Month; }
        private readonly Brush brush = new SolidBrush(Color.Blue);
    
        const int offset = 4;
    
        public Form1() {
            InitializeComponent();
            view.CellPainting += View_CellPainting;
        }
    
        private void View_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
            if (e.RowIndex < 0 || e.ColumnIndex <= endTimeColumn.Index) return;
            if (view.Rows[e.RowIndex].IsNewRow) return;
    
            object startValue = view[startTimeColumn.Index, e.RowIndex].Value;
            object endValue = view[endTimeColumn.Index, e.RowIndex].Value;
            if (!(startValue is DateTime) || !(endValue is DateTime)) return;
            DateTime startTime = (DateTime)startValue;
            DateTime endTime = (DateTime)endValue;
    
            int day = int.Parse(view.Columns[e.ColumnIndex].HeaderText);
            DateTime dayStart = new DateTime(gridYear, gridMonth, day);
            DateTime dayEnd = dayStart.AddDays(1);
            TimeSpan daySpan = dayEnd - dayStart;
    
            if (startTime < dayEnd && endTime >= dayStart) {
                e.Paint(e.CellBounds, DataGridViewPaintParts.All);
    
                int top = e.CellBounds.Top + 4;
                int bottom = e.CellBounds.Bottom - 4;
    
                int left = (int)(e.CellBounds.Width * (startTime - dayStart).TotalMinutes / daySpan.TotalMinutes);
                if (left < 0) left = 0;
                left = e.CellBounds.Left + left;
    
                int right = (int)(e.CellBounds.Width * (dayEnd - endTime).TotalMinutes / daySpan.TotalMinutes);
                if (right < 0) right = 0;
                right = e.CellBounds.Right - right;
    
                Rectangle face = Rectangle.FromLTRB(left, top, right, bottom);
                e.Graphics.FillRectangle(brush, face);
    
                e.Handled = true;
            }
        }
    

    enter image description here

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Viorel 125.6K Reputation points
    2024-06-08T10:34:30.7266667+00:00

    Try something like this:

    if( DateTime.TryParse ... )
    {
        int last_column = row.Cells.Count - 1;
    
        for( int c = offset; c <= last_column; c++ )
        {
            DateTime d = startBook.Date.AddDays( -startBook.Day + (c - offset) + 1 );
            row.Cells[c].Style.BackColor = d >= startBook.Date && d <= endBook.Date ? Color.Blue : Color.White;
        }
    }
    
    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.