0

If dates are available in database table, then the Calendar background color should be changed as a red color base on available event dates in a SQL table. Below is my design code.

<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
</asp:Calendar>
Selected Date: <asp:Label ID="Label1" runat="server" ></asp:Label>

 <br />
 <br />

  <asp:GridView ID="gv_Regdetails" runat="server" AllowSorting="True"
                AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid"
                Width="900px" PageSize="50">
                <RowStyle HorizontalAlign="Center" />
                <AlternatingRowStyle CssClass="alt" />
                <Columns>
                    <asp:TemplateField HeaderText="Sno">
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" Width="30px" />
                        <ItemStyle CssClass="th" HorizontalAlign="Center" Width="30px" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="EventName" HeaderText="EventName">
                        <ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" />
                        <HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Title" HeaderText="Title">
                        <ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" />
                        <HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" />
                    </asp:BoundField>

                    <asp:BoundField DataField="Description" HeaderText="Description">
                        <ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" />
                        <HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" />
                    </asp:BoundField>

                </Columns>
                <EmptyDataTemplate>
                    <center>
                                    <asp:Label ID="Label12" runat="server" Text="No Data avaliable for selected dates"
                                        ForeColor="Red"></asp:Label></center>
                </EmptyDataTemplate>
            </asp:GridView>

Below is my c# code and I am writing my logic in Calendar1_DayRender method.

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{        
    Label1.Text = Calendar1.SelectedDate.ToShortDateString();
    bo.Para1 = Label1.Text;
    DataTable dt = new DataTable();
    dt = bl.Get_detailsbydate(bo);
    gv_Regdetails.DataSource = dt;
    gv_Regdetails.DataBind();
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    DataTable dt = new DataTable();
    dt = bl.Get_detailsbydate(bo);
    string dates = dt.Rows[0]["EventDate"].ToString();

 //   if (e.Day.Date == dates)
//    { 
 //           e.Day.IsSelectable = false;
 //           e.Cell.ForeColor = System.Drawing.Color.Red;
//    }


}

1 Answer 1

0
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
   DataTable dt = new DataTable();
   dt = bl.Get_detailsbydate(bo);
   string dates = dt.Rows[0]["EventDate"].ToString();// Confirm if you  EventDate is in a same format to calendar

   if (e.Day.Date == dates)
   { 
        e.Day.IsSelectable = false;
        e.Cell.BackColor = System.Drawing.Color.Red;
   }


}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.