narasiman rao

narasiman rao

  • NA
  • 519
  • 767.6k

how display the selected month date in datagridview usingc#

Feb 13 2013 8:07 AM


        DateTime dt1 = dateTimePicker1.Value;
        dt1 = new DateTime(dt1.Year, dt1.Month, 1); // get first day of selected month
        DateTime dt2 = dt1.AddMonths(1); // get first day of next month
        int numDays = (dt2 - dt1).Days;

        if (dataGridView1.RowCount < numDays)
        {
            dataGridView1.RowCount = numDays;
        }

        int row  = 0;

        while (dt1 < dt2)
        {
            dataGridView1.Rows[row].Cells[1].Value = dt1.ToString("dd/MM/yyyy");
            dt1 = dt1.AddDays(1);
            row++;
        }




 I work out your code, but when i run i get the error as follows;

 Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

the  above error occurs in below line as follows;

dataGridView1.Rows[row].Cells[1].Value = dt1.ToString("dd/MM/yyyy");

please help me.

Thanks & regards,
rao.







Answers (2)