Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Condition:Selected Month All Dates want to display Datagridview and dont want other month date in datagridview.

selected month date will display in datagrdiveiw using Month Calendar in Csharp.

For that code as follows;
C#
private void Faculty_Available_Calendar_DateChanged(object sender, DateRangeEventArgs e)
{
    DateTime dt1 = Faculty_Available_Calendar.SelectionStart;
   dt1 = new DateTime(dt1.Year, dt1.Month, 1);
    DateTime dt2 = dt1.AddMonths(1);
    int numDays = (dt2 - dt1).Days;
    if (DGVCalendar.RowCount < numDays)
    {
        DGVCalendar.RowCount = numDays;
    }
    int row = 0;
     while (dt1 < dt2)
    {
         DGVCalendar.Rows[row].Cells[0].Value = dt1.ToString("dd-MM-yy");
          dt1 = dt1.AddDays(1);
        row++;
    }
}
when i select April Month That Month Date is display and March month date 31/03/2013 also display why?

from my code what is the mistake i made?

Please help me.

Output as follows in Datagridview

when i select april month that month date only display in datagridview.

Month (seleting April Month)
1/4/2013
to 30/4/2013
and
31/3/2013.

Condition:Selected Month All Dates want to display Datagridview and dont want other month date in datagridview.
Posted
Updated 22-Feb-13 0:54am
v2

1 solution

The problem is that you are not clearing the DGV down each time. You must have selected March on a previous run (31 days) and then April (30 days) so the last day in March is not being overwritten by the new month.

Use
DGVCalendar.Rows.Clear();
before the while loop after the int numDays ...
Of course the number of rows will now always be zero so you can tidy up the if statement immediately after that as well
 
Share this answer
 
v2
Comments
[no name] 22-Feb-13 8:09am    
Yo said that before while loop add the DGVCalendar.Rows.Clear();

I added in the below code it is correct please help me.

private void Faculty_Available_Calendar_DateChanged(object sender, DateRangeEventArgs e)
{
DateTime dt1 = Faculty_Available_Calendar.SelectionStart;
dt1 = new DateTime(dt1.Year, dt1.Month, 1);
DateTime dt2 = dt1.AddMonths(1);
int numDays = (dt2 - dt1).Days;
if (DGVCalendar.RowCount < numDays)
{
DGVCalendar.RowCount = numDays;
}
int row = 0;
DGVCalendar.Rows.Clear();
while (dt1 < dt2)
{
DGVCalendar.Rows[row].Cells[0].Value = dt1.ToString("dd-MM-yy");
dt1 = dt1.AddDays(1);
row++;
}
}


Whether i added the DGVCalendar.Rows.Clear(); before the while loop correctly.

please help me.

Thanks & Regards,
Narasiman P
CHill60 22-Feb-13 8:21am    
Sorry that was my fault - I should have said to put the line before if(DGVCalendar.RowCount ... I'll update my answer and do it right this time :-)

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