Highlight wpf calendar alternately - c#

When a date is selected in DatePicker tool or type one date in TextBox,
how do highlight wpf calendar cells every three dates after selected date alternately? (3 dates highlight, 3 dates not highlight and this continues..).
I used this code, but intervals select one day every 3 days on MontlyCalendar:
DateTime a = new DateTime();
a = DateTime.Parse(myDatePicker1.Text);
DateTime h = new DateTime();
h = DateTime.Parse(myDatePicker2.Text);
for (DateTime f = a; f < h; f=f.AddDays(3))
{
MonthlyCalendar.SelectedDates.Add(f);
}

I hope this solves your problem
private int cant = 0;
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (cant < 3)
{
//highlight
cant++;
}
else if (cant < 6)
{
//not highlight
cant++;
}
else
{
cant = 0;
}
}

This is my solution:
private void btnClick2_Click(object sender, RoutedEventArgs e)
{
string dateString1, dateString2, format;
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime t = DateTime.Parse(datePicker1.Text);
DateTime End = DateTime.Parse(datePicker2.Text);
DateTime g = t.AddDays(6);
TimeSpan ts = (g - t);
for (DateTime i = t; i <= End; i += ts)
{
DateTime r = i.AddDays(2);
dateString1 = i.ToString("MM/dd/yyyy");
dateString2 = r.ToString("MM/dd/yyyy");
format = "d";
DateTime a = DateTime.ParseExact(dateString1, format, provider);
DateTime b = DateTime.ParseExact(dateString2, format, provider);
myCalendar.SelectedDates.AddRange(a, b);
}
}

Related

c# Web app How to get no of working days exclude weekends and public holidays

Background information
Just starting out to learn C#, im trying to build a simple web app that calculate the no of working day(s) between 2 dates.
The UI of the web app
The basic logic is when the user input a date (ie 01/05/2018) and click the button.It will calculate the total number of working day (exclude weekends and public holidays).
The problem now is the calculation isnt accurate ie between 23/05/2018 & 31/05/2018 it shows 6, it should be 7 days. And it doesnt take the dates into consideration during calculation
namespace testtest
{
public partial class First : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//on btn click
protected void Button1_Click(object sender, EventArgs e)
{
string dt = TextBox1.Text;
DateTime dtDDMMYYYY = ParseDate(dt);
string dt2 = TextBox2.Text;
DateTime dtDDMMYYYY2 = ParseDate(dt2);
List<DateTime> list = new List<DateTime> {
DateTime.ParseExact("04/05/2018", "dd/MM/yyyy",
CultureInfo.InvariantCulture) };
DaysLeft(dtDDMMYYYY, dtDDMMYYYY2, true, list);
}
public DateTime ParseDate(string date)
{
DateTimeFormatInfo dateFormatProvider = new DateTimeFormatInfo();
dateFormatProvider.ShortDatePattern = "dd/MM/yyyy";
return DateTime.Parse(date, dateFormatProvider);
}
public int DaysLeft(DateTime startDate, DateTime endDate, Boolean
excludeWeekends, List<DateTime> excludeDates)
{
int count = 0;
for (DateTime index = startDate; index < endDate; index =
index.AddDays(1))
{
if (excludeWeekends && index.DayOfWeek != DayOfWeek.Sunday &&
index.DayOfWeek != DayOfWeek.Saturday)
{
bool excluded = false; ;
for (int i = 0; i < excludeDates.Count; i++)
{
if (index.Date.CompareTo(excludeDates[i].Date) == 0)
{
excluded = true;
break;
}
}
if (!excluded)
{
count++;
}
}
}
result.Text = count.ToString();
return count;
}
}
}
Keep it simple
public int DaysLeft(DateTime startDate, DateTime endDate, Boolean excludeWeekends, List<DateTime> excludeDates) {
int count = 0;
for (DateTime index = startDate; index <= endDate; index = index.AddDays(1)) {
if (excludeWeekends && (index.DayOfWeek == DayOfWeek.Sunday || index.DayOfWeek == DayOfWeek.Saturday))
continue;
if (excludeDates.Contains(index.Date))
continue;
count++;
}
return count;
}
If the date is a weekend and excludeWeekends flagged, continue on to next date, if date is included in excludeDates continue, else count the day.

How to paint 5 days in a calendar?

I'm getting the start date of a workday from a database, so I can display it on a calendar.
In the calendar you should only show the start date plus 7 days and these should be in a different color to know that this is your work week.
How can I do it? I have the following
private void calFecha_DayRender(object source, DayRenderEventArgs e)
{
if (ddloperadores.SelectedValue != "Todos")
{
DataTable asistencia = OperadoresAsistencia((int)WAPS.Globals.ConvertTo(txtNumOperador.Text, 0)).Tables[0];
if (asistencia.Rows.Count > 0)
{
DataRow iRow = asistencia.Rows[0];
string Tipo = iRow["Tipo"].ToString();
Tipo = asistencia.Rows[0]["Tipo"].ToString();
DateTime FechaJornada = DateTime.Now;
string FechaJ = FechaJornada.AddDays(8).ToShortDateString();
TextBox1.Text = FechaJ;
if (Tipo == "1")
{
e.Cell.BackColor = System.Drawing.Color.DarkOrange;
}
}
}
}

Comparing two dates one from the database and datetime.now

I have a gridview and I would like to highlight the days which has difference > 10.
What I have now
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime dateNow = DateTime.Now;
string dateCreated = e.Row.Cells[5].Text;
DateTime dc = Convert.ToDateTime(dateCreated);
TimeSpan difference = dateNow - dc;
if (difference.TotalDays > 0)
{
e.Row.BackColor = System.Drawing.Color.Cyan; // This will make row back color red
}
}
}
Date format of the one saved onto my database is dd-MM-YYYY and it is saved as nvarchar.
Use this:
int minutes = (int)DateTime.Now.Subtract(dc).TotalMinutes;
or days
int days = (int)DateTime.Now.Subtract(dc).TotalDays;
Or seconds, miliSeconds, Weeks, ....
To get date from your string do this:
string dateCreated = "01-01-2017";
string[] dcArray = dateCreated.Split('-');
DateTime dc = new DateTime(int.Parse(dcArray[2]), int.Parse(dcArray[1]), int.Parse(dcArray[0]));
You should parse DateTime like that:
DateTime dc = DateTime.ParseExact(dateCreated, "dd-MM-yyyy", CultureInfo.InvariantCulture);
Victor Leontyev is right on with the parsing. But then you also have to tweak you logic:
if (difference.TotalDays > 0)
to
if (Math.Abs(difference.TotalDays) > 10)

C# - Execute on Datagrid the Selected Date From and To

I am creating some schedule output using dateTimePicture in C# toolbox(Format of Short) on the datagrid. I just want is when the user choose the date "From" and "To" and click the button execute, the dategrid will automatically output the date starting "From" and end on "To". Any ideas? Please just comment your ideas below. Thank you guys!
Here's my code
private void btn_execute_Click(object sender, EventArgs e)
{
String from = date_from.Text;
String to = date_to.Text;
int count;
for(count = 0; count < to; count++)
{
dgv_result.Rows.Add(1);
dgv_result.Rows[count].Cells[0].Value = from;
}
}
Try this
var fromDate = DateTime.Now; // set here your from date
var toDate = DateTime.Now.AddDays(7); // set here your to date
for (DateTime date = fromDate; date <= toDate; date = date.AddDays(1))
{
dataGridView1.Rows.Add(date);
}
EDIT:
If you are using DateTimePicker for toDate and fromDate then try this
var fromDate = date_from.Value; // Don't use date_from.Text as you mentioned in comments
var toDate = date_to.Value;
for (DateTime date = fromDate; date <= toDate; date = date.AddDays(1))
{
dataGridView1.Rows.Add(date);
}

Telerik Rad Calendar - Is Changing Data Source Possible?

I'm trying to change the dates displayed in a RadCalendar. For example, I want it to begin 2 weeks before the current date and ends two weeks after the current date. Is it possible?
I was able to change the text displayed in the cells (to display the "new" date) but the "OnClick" methods still sends the "old" date.
OnDayRender I added :
e.Cell.Text = "" + _calStartDate.Day.ToString() + "";
_calStartDate = _calStartDate.AddDays(1);
But the calendar still thinks that the new dates are the old one, so the "SelectedDate" method returns the "old" date and the date selected is not the current date.
Is there a way to just pass a new list of dates, which would be easier?
UPDATE / Solution:
I was able to make it work like that:
private int rowCounter = 0;
private int rowHeaderCnt = 0;
private DateTime _startDate;
private DateTime _endDate;
private DateTime _calStartDate;
private DateTime _calEndDate;
protected void radCalendar_DayRender(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e)
{
TableRow tr = (TableRow)e.Cell.Parent;
Table table = (Table)tr.Parent;
if (e.Day.Date.CompareTo(_calStartDate) < 0)
{
((TableRow)(e.Cell.Parent)).Style["display"] = "none";
}
else if (e.Day.Date.CompareTo(_calEndDate) > 0)
{
((TableRow)(e.Cell.Parent)).Style["display"] = "none";
}
else if (e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
{
// This part will change the week number cell (if you don't display it, hide this part)
rowCounter++;
TableCell cellRowHeader = ((TableRow)(e.Cell.Parent)).Cells[0];
cellRowHeader.Text = rowCounter.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
radCalendar.RangeMinDate = _startDate;
radCalendar.RangeMaxDate = _endDate;
}
protected void radCalendar_HeaderCellRender(object sender, Telerik.Web.UI.Calendar.HeaderCellRenderEventArgs e)
{
if (e.HeaderType == Telerik.Web.UI.Calendar.HeaderType.Row)
{
rowHeaderCnt++;
e.Cell.Text = " " + rowHeaderCnt;
}
if (e.HeaderType == Telerik.Web.UI.Calendar.HeaderType.Column)
{
TableRow row = ((TableRow)(e.Cell.Parent));
row.Cells[0].Text = " " + StringUtil.getStringByLanguage("Week", "Sem.") + " ";
}
}
protected void raCalendar_SelectionChanged(object sender, Telerik.Web.UI.Calendar.SelectedDatesEventArgs e)
{
DateTime startDate = new DateTime();
DateTime endDate = new DateTime();
String url = String.Empty;
if (e.SelectedDates.Count == 1)
{
startDate = e.SelectedDates[0].Date;
endDate = e.SelectedDates[0].Date;
}
else
{
startDate = e.SelectedDates[0].Date;
endDate = e.SelectedDates[e.SelectedDates.Count - 1].Date;
}
// ... add code here with startDate and endDate
}
public void initCalendar(DateTime startDate, DateTime endDate)
{
this._startDate = startDate;
this._endDate = endDate;
this._calStartDate = startDate;
this._calEndDate = endDate;
while (this._calStartDate.DayOfWeek != DayOfWeek.Sunday)
{
this._calStartDate = this._calStartDate.AddDays(-1);
}
while (this._calEndDate.DayOfWeek != DayOfWeek.Saturday)
{
this._calEndDate = this._calEndDate.AddDays(1);
}
}
Based on my attempts, you can get very close to this setup. You have to use some trickery though, as the functionality, as far as I can tell, is not built into the calendar to only display the dates as you have asked for.
On page load:
protected void Page_Load(object sender, EventArgs e)
{
RadCalendar1.RangeMinDate = DateTime.Now.AddDays(-14);
RadCalendar1.RangeMaxDate = DateTime.Now.AddDays(14);
RadCalendar1.FirstDayOfWeek = (FirstDayOfWeek)DateTime.Now.AddDays(-14).DayOfWeek;
}
On day render:
protected void RadCalendar1_DayRender1(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e)
{
if (e.Day.Date >= RadCalendar1.RangeMinDate.Date && e.Day.Date <= RadCalendar1.RangeMaxDate.Date)
{
e.Cell.Visible = true;
}
else
{
e.Cell.Visible = false;
}
}
This will get you an initial calendar load that shows 2 weeks back and 2 weeks forward and only allow the user to select inside that date. What it does not do, and I'd guess to be a separate question, is it does not execute the hiding of the dates outside the range when you page to the following month.
You must be aware that the format for the calendar must be 42 days, as per the design of the tool itself. That is why you see the blank line on top, as we are hiding those days. To my knowledge you can not remove them, only hide them or display them but not allow them to be clicked.

Categories

Resources