locked
How to insert selected date in a data grid from a calendar in asp.net c# RRS feed

  • Question

  • i have calendar and a data grid view in form.when i select any date form the calendar that date have to display in the data grid one by one.
    Tuesday, April 30, 2019 7:10 AM

Answers

  • Hi Sherin123,

    There is no control "calendar" in Winforms. What is the "calendar"? Is it the "DateTimePicker"? If so, here I have a simple demo you can refer to:

        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumn dateColumn = new DataGridViewTextBoxColumn();
            dateColumn.Name = "Date";
            dateColumn.DataPropertyName = "Date";
            dateColumn.HeaderText = "Date";
            dataGridView1.Columns.Add(dateColumn);
        }
    
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string date = dateTimePicker1.Value.ToLongDateString();
            DataGridViewRow dr = new DataGridViewRow();
            dr.CreateCells(dataGridView1);
            dr.Cells[0].Value = date;
            dataGridView1.Rows.Add(dr);
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sherin123 Tuesday, April 30, 2019 10:04 AM
    • Unmarked as answer by Sherin123 Tuesday, April 30, 2019 10:05 AM
    • Marked as answer by Sherin123 Tuesday, April 30, 2019 10:05 AM
    Tuesday, April 30, 2019 8:28 AM
  • Hi Sherin123,

    What is the type of your project? An ASP.Net or a Winforms? Before handling the issue, I need to confirm it.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 30, 2019 7:43 AM

All replies

  • Hi Sherin123,

    What is the type of your project? An ASP.Net or a Winforms? Before handling the issue, I need to confirm it.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 30, 2019 7:43 AM
  • its winforms
    • Marked as answer by Sherin123 Tuesday, April 30, 2019 8:12 AM
    • Unmarked as answer by Sherin123 Tuesday, April 30, 2019 8:12 AM
    Tuesday, April 30, 2019 8:12 AM
  • Hi Sherin123,

    There is no control "calendar" in Winforms. What is the "calendar"? Is it the "DateTimePicker"? If so, here I have a simple demo you can refer to:

        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumn dateColumn = new DataGridViewTextBoxColumn();
            dateColumn.Name = "Date";
            dateColumn.DataPropertyName = "Date";
            dateColumn.HeaderText = "Date";
            dataGridView1.Columns.Add(dateColumn);
        }
    
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string date = dateTimePicker1.Value.ToLongDateString();
            DataGridViewRow dr = new DataGridViewRow();
            dr.CreateCells(dataGridView1);
            dr.Cells[0].Value = date;
            dataGridView1.Rows.Add(dr);
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sherin123 Tuesday, April 30, 2019 10:04 AM
    • Unmarked as answer by Sherin123 Tuesday, April 30, 2019 10:05 AM
    • Marked as answer by Sherin123 Tuesday, April 30, 2019 10:05 AM
    Tuesday, April 30, 2019 8:28 AM
  • Thank you so much sir.And there is a calendar tool i n win forms  its called month calendar.
    Tuesday, April 30, 2019 10:19 AM
  • i need one more help  how can i add serial number to the data grid when i select date ????please help me to find this
    Tuesday, April 30, 2019 10:20 AM
  • Hi Sherin123,

    Sorry for my mistake, please refer to the following code:

        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
    
        int num = 0;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumn dateNo = new DataGridViewTextBoxColumn();
            dateNo.Name = "No";
            dateNo.DataPropertyName = "No";
            dateNo.HeaderText = "No.";
            dataGridView1.Columns.Add(dateNo);
            DataGridViewTextBoxColumn dateColumn = new DataGridViewTextBoxColumn();
            dateColumn.Name = "Date";
            dateColumn.DataPropertyName = "Date";
            dateColumn.HeaderText = "Date";
            dataGridView1.Columns.Add(dateColumn);
        }
    
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            string date = monthCalendar1.SelectionStart.ToString("yyyy-MM-dd");
            DataGridViewRow dr = new DataGridViewRow();
            dr.CreateCells(dataGridView1);
            dr.Cells[0].Value = ++num;
            dr.Cells[1].Value = date;
            dataGridView1.Rows.Add(dr);
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 2, 2019 1:36 AM