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;
}
}
}
}
Related
I have a column called Review Date from MySQL. Formatted as dd/mm/yyyy.
I simply show it on the dataGridView.
enter image description here
I am trying to create a statement to compare today's date to the dataGrid values.
Here is what I have tried:
private void data_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DateTime today = DateTime.Today;
string todayDate = today.ToString("d/M/yyyy");
foreach (DataGridViewRow Myrow in data.Rows)
{
// Cell 6 is the review_date
if (Myrow.Cells[6].Value.ToString() < todayDate)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
}
But I get an error:
Operator '<' cannot be applied to operands of type 'string' and 'string'
You should use Date objects.
DateTime today = DateTime.Now;
foreach (DataGridViewRow Myrow in data.Rows)
{
DateTime dt = DateTime.Parse(Myrow.Cells[6].Value.ToString());
if (dt > today)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
Also, quick tip.
If you'd like to add days or months to your date. Try this...
//Add days
if (dt > today.AddDays(10))
//Add months
if (dt > today.AddMonths(12))
You can't do a less than operator on strings like that. You need to do the comparison the date objects. Something like this perhaps.
private void data_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DateTime today = DateTime.Today.Date; //Used .Date to cut off time too
foreach (DataGridViewRow Myrow in data.Rows)
{
var GridDate = DateTime.Today.Date;
DateTime.TryParse(Myrow.Cells[6], out GridDate); //parse string date
//Cell 6 is the review_date
if (GridDate < todayDate)
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
}
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.
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);
}
}
I am populating a DataGridView with a semicolon-delimited text file like so:
private void ExistingAppntmntRecs_Load(object sender, EventArgs e)
{
DataTable dt = SeparatedValsFileToDataTable(APPOINTMENTS_FILE_NAME, ";");
dataGridViewExistingAppntmntRecs.DataSource = dt;
}
// from http://stackoverflow.com/questions/39434405/read-csv-to-datatable-and-fill-a-datagridview (Frank)
public static DataTable SeparatedValsFileToDataTable(string filename, string separatorChar)
{
var table = new DataTable("Filecsv");
using (var sr = new StreamReader(filename, Encoding.Default))
{
string line;
var i = 0;
while (sr.Peek() >= 0)
{
try
{
line = sr.ReadLine();
if (string.IsNullOrEmpty(line)) continue;
var values = line.Split(new[] { separatorChar }, StringSplitOptions.None);
var row = table.NewRow();
for (var colNum = 0; colNum < values.Length; colNum++)
{
var value = values[colNum];
if (i == 0)
{
table.Columns.Add(value, typeof(String));
}
else
{ row[table.Columns[colNum]] = value; }
}
if (i != 0) table.Rows.Add(row);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
i++;
}
}
return table;
}
What I want to do now is to color the rows which have an EndDate value within two months of the current date yellow, within one month orange, and those with a date in the past (meaning they have lapsed) red.
There is a PostPaint event which may work, but I don't know how to examine cell contents within the row in that event handler:
private void dataGridViewExistingAppntmntRecs_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
// What now?
}
Here is the contents of the file that is read (bogus/test data, with a header row prepended):
person;phone;email;org;addr1;addr2;city;st8;zip;visitNum;success;adSizeLoc;meetingLoc;meetingDate;meetingTime;adBeginMonth;adBeginYear;adEndMonth;adEndYear;Notes
B.B. King;2221113333;bbking#blues.com;Virtuosos;1234 Wayback Lane;;Chicago;IL;98765;1;false;Full Page Inside Front Cover;Same as Org Address;4/5/2017 2:03:12 PM;9:00 am;May;2017;May;2018;Lucille was her name
Linda Ronstadt;55577889999;rhondalinstadt#eaglet.com;Eagles Singer;La Canada;;Los Angeles;CA;99988;1;false;Full page Inside Back Cover;Same as Org Address;4/5/2017 2:05:23 PM;9:00 am;May;2017;May;2018;She had some good stuff
If the adEndMonth + adEndYear date equates to 2 months or less away, the entire row should be yellow; if 1 month or less away, orange; if today or in the past, paint it red. Finally, if one of the Rolling Stones is running the app, paint it black.
Here is some pseudocode for the PostPaint event, with "TODO:" where I don't know what to do:
private void dataGridViewExistingAppntmntRecs_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DateTime twoMonthsLimit = DateTime.Now.AddMonths(2);
DateTime oneMonthLimit = DateTime.Now.AddMonths(1);
int endYear = // TODO: assign adEndYear value
int endMonth = // TODO: assign adEndMonth value
DateTime endDate = new DateTime(endYear, endMonth, 1);
if (twoMonthsLimit > endDate) // TODO: paint row yellow
if (oneMonthLimit > endDate) // TODO: paint row orange
if (endDate < DateTime.Now) // TODO: paint row red
}
If the goal is too simply, highlight the rows that fall within certain dates, then changing the rows background color may be an easier option. Repainting the rows may be unnecessary. My solution simply changes the rows background color depending on the dates in the “endMonth” and “endYear” columns.
The cell formatting is an option however, it will fire often and placing this “Coloring” checks every time a cell is changed or displayed is unnecessary. If the rows have already been “Colored”, then the only things to look for is when new rows are added or the values in the “endMonth” or “endYear” columns are changed.
Below is code that simply loops through the DataGridView and sets each row color based on the criteria you described. The logic to get a rows color is fairly straightforward (minus the paint it black). If the date is greater than two months forward from today’s date then leave the background color white. If the date is greater than 1 month but less than 2 months then color the row yellow… etc.
I used a similar approach to read the text file and create the DataTable. Hope this helps.
DataTable dt;
string filePath = #"D:\Test\Artist.txt";
char delimiter = ';';
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
dt = GetTableFromFile(filePath, delimiter);
dataGridViewExistingAppntmntRecs.DataSource = dt;
UpdateDataGridColors();
}
private DataTable GetTableFromFile(string filePath, char delimiter) {
List<string[]> allArtists = GetArtistList(filePath, delimiter);
DataTable dt = GetTableColumns(allArtists[0]);
int totalCols = dt.Columns.Count;
DataRow dr;
for (int i = 1; i < allArtists.Count; i++) {
string[] curArtist = allArtists[i];
dr = dt.NewRow();
for (int j = 0; j < totalCols; j++) {
dr[j] = curArtist[j].ToString();
}
dt.Rows.Add(dr);
}
return dt;
}
private List<string[]> GetArtistList(string inFilePath, char inDelimiter) {
string pathToFile = inFilePath;
char delimiter = inDelimiter;
List<string[]> listStringArrays = File.ReadAllLines(pathToFile).Select(x => x.Split(delimiter)).ToList();
return listStringArrays;
}
private DataTable GetTableColumns(string[] allHeaders) {
DataTable dt = new DataTable();
foreach (string curHeader in allHeaders) {
dt.Columns.Add(curHeader, typeof(string));
}
return dt;
}
private Color GetColorForRow(DataRowView dr) {
// paint it black ;-)
if (dr[0].ToString().Equals("Rolling Stones")) {
return Color.Black;
}
DateTime rowDate;
DateTime dateNow = DateTime.Now;
DateTime twoMonthsLimit = dateNow.AddMonths(2);
DateTime oneMonthLimit = dateNow.AddMonths(1);
if (dr != null) {
string rowStringMonth = dr[17].ToString();
string rowStringYear = dr[18].ToString();
string rowStringDate = "1/" + rowStringMonth + "/" + rowStringYear;
if (DateTime.TryParse(rowStringDate, out rowDate)) {
if (rowDate > twoMonthsLimit)
return Color.White;
if (rowDate > oneMonthLimit)
return Color.Yellow;
if (rowDate > dateNow)
return Color.Orange;
if (rowDate.Month == dateNow.Month && rowDate.Year == dateNow.Year)
return Color.Orange;
// this row date is less than todays month date
return Color.Red;
} // else date time parse unsuccessful - ignoring
}
// date is null
return Color.White;
}
private void UpdateDataGridColors() {
Color rowColor;
DataRowView dr;
foreach (DataGridViewRow dgvr in dataGridViewExistingAppntmntRecs.Rows) {
dr = dgvr.DataBoundItem as DataRowView;
if (dr != null) {
rowColor = GetColorForRow(dr);
dgvr.DefaultCellStyle.BackColor = rowColor;
if (rowColor == Color.Black)
dgvr.DefaultCellStyle.ForeColor = Color.White;
}
}
}
private void dataGridViewExistingAppntmntRecs_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 17 || e.ColumnIndex == 18) {
//MessageBox.Show("End Date Changed");
UpdateDataGridColors();
}
}
private void dataGridViewExistingAppntmntRecs_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {
UpdateDataGridColors();
}
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.