Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a database with columns cal_id, event name, event start time, event finish time, event date.
but i am getting the point how we can retrieve or show that data in the calendar control.
as in asp.net, we use calender1_dayRender() to retrieve data from the database. how we can show calendar data from the database to calender in c# winform.

What I have tried:

i found a code for asp.net
but dayRender is not available in winform calender.

C#
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)   
{  
  
    Literal l = new Literal(); //Creating a literal  
    l.Visible = true;  
    l.Text = "<br>"; //for breaking the line in cell  
    e.Cell.Controls.Add(l); //adding in all cell  
  
    da = new SqlDataAdapter("select * from Events", con);  
    DataTable dt = new DataTable();  
    da.Fill(dt);  
    foreach(DataRow dr in dt.Rows)   
    {  
        string x = dr[1].ToString();  
  
        if (dr[1].ToString() == e.Day.Date.ToString()) //comparision  
        {  
            Label lb = new Label();  
            lb.Visible = true;  
            lb.Text = dr[0].ToString();  
  
            string a = lb.Text;  
            e.Cell.Controls.Add(lb);  
            e.Cell.BackColor = System.Drawing.Color.OrangeRed; // changing cell color  
            e.Cell.ToolTip = dr[0].ToString(); //adding tooltip  
        }  
    }
Posted
Updated 14-Apr-20 19:48pm
v2
Comments
Wendelius 14-Apr-20 23:23pm    
What calendar control you're using? MonthCalendar?
Member 14192879 15-Apr-20 1:28am    
yes, month calendar. is there any other calendar available?

1 solution

Quote:
how we can show calendar data from the database to calender in c# winform.


Take a look here:
Select a Range of Dates in MonthCalendar Control - Windows Forms | Microsoft Docs[^]
Display Specific Days in Bold with MonthCalendar Control - Windows Forms | Microsoft Docs[^]
Change MonthCalendar Control's Appearance - Windows Forms | Microsoft Docs[^]
Display More than One Month in MonthCalendar Control - Windows Forms | Microsoft Docs[^]

Quote:
To select a range of dates
1.Create DateTime objects that represent the first and last dates in a range.
C#
DateTime projectStart = new DateTime(2001, 2, 13);  
DateTime projectEnd = new DateTime(2001, 2, 28);

2. Set the SelectionRange property.
C#
monthCalendar1.SelectionRange = new SelectionRange(projectStart, projectEnd);

or
3. Set the SelectionStart and SelectionEnd properties.
C#
monthCalendar1.SelectionStart = projectStart;  
monthCalendar1.SelectionEnd = projectEnd;


Conclusion: all you need to do is to get dates (start and end) from database and use one of above ways to select range of dates.
 
Share this answer
 
Comments
Member 14192879 15-Apr-20 1:57am    
can we add meeting details below the date in month calender?
Maciej Los 15-Apr-20 2:02am    
I don't know. You can always display the details of event below the calendar.

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