c# How to update variable value in loop - c#

I've try to loop and from the below code, The problem is when it loop back to “IEnumerable selectedRows =”. The value of DateStart and DateEnd remain the same “9/1/2003 2:00:00” and “9/1/2003 2:59:00” respectively as its initial value. Why the “IEnumerable selectedRows =” do not update the DateTime value? Please help and thank you.
IEnumerable<DataRow> selectedRows =
dtData
.AsEnumerable()
.Where(row =>
row.Field<DateTime>("Date") >= DateStart
&& row.Field<DateTime>("Date") <= DateEnd);
foreach (DataRow row in selectedRows)
{
//Do some stuffs
}
//store the result in DataTable
DataRow dtDataRowResult = dtDataResult.NewRow();
dtDataRowResult[0] = DateStart;
dtDataRowResult[1] = result1;
dtDataRowResult[2] = result2;
dtDataResult.Rows.Add(dtDataRowResult);
//when code go this line below,
//it should be added one hour from the previous hour,
//so now it is “9/1/2003 3:00:00”
DateStart = DateStart.AddHours(1);
//and the DateEnd should be added to “9/1/2003 3:59:00”
DateEnd = DateStart + TimeSpan.FromMinutes(59);
//The problem is when it loop back to
//“IEnumerable<DataRow> selectedRows =”.
//The value of DateStart and DateEnd remain the same
//“9/1/2003 2:00:00” and “9/1/2003 2:59:00” respectively
//as the initial value. Why the
//“IEnumerable<DataRow> selectedRows =” not updated the
//DateTime value? Please help and thank you.
The below is the screenshot image of the example code

Let me see if I've produced a [mcve] that demonstrates your problem:
DateTime dateTime = new DateTime(2023, 1, 9, 2, 0, 0);
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Date", typeof(DateTime));
DataRow dataRow = dataTable.NewRow();
dataRow[0] = dateTime;
dataTable.Rows.Add(dataRow);
Console.WriteLine($"{dateTime} == {(DateTime)dataTable.Rows[0][0]}");
dateTime += TimeSpan.FromMinutes(59.0);
Console.WriteLine($"{dateTime} != {(DateTime)dataTable.Rows[0][0]}");
When I run that I get this output:
2023/01/09 02:00:00 == 2023/01/09 02:00:00
2023/01/09 02:59:00 != 2023/01/09 02:00:00
The value in the DataTable does not update when I update the dateTime variable.
This is because when dateTime += TimeSpan.FromMinutes(59.0); is called the value in the variable dateTime is updated, but the variable stored in the DataTable is a copy of the original dateTime value. It's not linked in any way.
To make it update you would need to run this code:
dataTable.Rows[0][0] = dateTime;
Now we can see it is the same with this:
Console.WriteLine($"{dateTime} == {(DateTime)dataTable.Rows[0][0]}");
That produces:
2023/01/09 02:59:00 == 2023/01/09 02:59:00

Related

How to display all Fridays Date between two dates

How to get a Friday date from the given start date and end date,
For Example:
25/03/2021 - starting date
14/08/2021 - endind date
I have a class
public static class DateUtils
{
public static List<DateTime> GetWeekdayInRange(this DateTime from, DateTime to, DayOfWeek day)
{
const int daysInWeek = 7;
var result = new List<DateTime>();
var daysToAdd = ((int)day - (int)from.DayOfWeek + daysInWeek) % daysInWeek;
do
{
from = from.AddDays(daysToAdd);
result.Add(from);
daysToAdd = daysInWeek;
}
while (from < to);
return result;
}
}
That is how i call it in main method:
var from = DateTime.Today; // 25/8/2019
var to = DateTime.Today.AddDays(23); // 23/9/2019
var allFriday = from.GetWeekdayInRange(to, DayOfWeek.Friday);
Console.WriteLine(allFriday);
Console.ReadKey();
Error i get:
System.Collections.Generic.List`1[System.DateTime]
I am new and still learning, how do I call in the main method so that my output be like all dates(fridays) between the range?
Link I followed
To Answer your question, instead of printing allFridays in one go, iterate over each element of list i.e allFridays, convert into string and then print
foreach(var friday in allFridays)
Console.WriteLine(friday);
Why you are getting System.Collections.Generic.List[System.DateTime] ?
Console.WriteLine(), for non primitive type by default calls
.ToString() function which prints type of it(if it is not overridden). In your case, you
need an individual date not a type of List, so you need to iterate
each DateTime from the list and print each date.
One Liner solution:
Console.WriteLine(string.Join(Environment.NewLine, allFridays));
Alternate solution:
public static List<DateTime> GetWeekdayInRange(this DateTime #from, DateTime to, DayOfWeek day)
{
//Create list of DateTime to store range of dates
var dates = new List<DateTime>();
//Iterate over each DateTime and store it in dates list
for (var dt = #from; dt <= to; dt = dt.AddDays(1))
dates.Add(dt);
//Filter date based on DayOfWeek
var filteredDates = dates.Where(x => x.DayOfWeek == day).ToList();
return filteredDates;
}
...
var #from = DateTime.Today; // 25/8/2019
var to = DateTime.Today.AddDays(23); // 23/9/2019
var allFriday = #from.GetWeekdayInRange(to, DayOfWeek.Friday);
Console.WriteLine(string.Join(Environment.NewLine, allFridays));
.NET FIDDLE
Since in your Usage section, you have successfully get the result via GetWeekdayInRange. You can print the dates with these methods:
Method 1:
allFriday.ForEach(x => Console.WriteLine(x.ToShortDateString()));
Method 2:
foreach (var friday in allFriday)
{
Console.WriteLine(friday.ToShortDateString());
}
Method 3:
for (var i = 0; i < allFriday.Count(); i++)
{
Console.WriteLine(allFriday[i].ToShortDateString());
}
Note: ToShortDateString() is one of the methods to display Date string. You can define your desired Date pattern with ToString().

Parse DateTime column from datatable that is bad

I have converted a very large csv file to a datatable and now I am parsing each column. I have run into a problem where the data in a specific column is not correct. It is suppose to be a date, ie 1/19/2020, but it has 1/1/0001 so it is crashing my attempt to parse. What I am attempting to do is to remove rows in the table before I write it back out.
DateTime checkDate = new DateTime(2018, 01, 01, 0, 0, 0);
for(int i = dt.Rows.Count-1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
DateTime AccountInformationDate = DateTime.Parse(row["AccountInformationDate"].ToString());
if (DateTime.Compare(checkDate, AccountInformationDate) > 0)
{
row.Delete();
counter_skipped++;
}
}
dt.AcceptChanges();
I get an exception when it tries to parse the date.
Use DateTime.TryParse to check if the input is valid. This solution assumes that you only want to keep the rows with valid DateTime that is later than checkDate; adjust it to your needs as necessary.
DateTime checkDate = new DateTime(2018, 01, 01, 0, 0, 0);
for(int i = dt.Rows.Count-1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
DateTime AccountInformationDate;
bool dateIsValid = DateTime.TryParse(row["AccountInformationDate"].ToString(), out AccountInformationDate);
if (!dateIsValid || (dateIsValid && DateTime.Compare(checkDate, AccountInformationDate) > 0))
{
row.Delete();
counter_skipped++;
}
}
dt.AcceptChanges();
You can use DateTime.TryParse to check whether the date string is correct.

how to compare date in database column & date_now, and put result in a column?

i have a table in SqlServer which contain's: (ID, item_name, date_time_added)
i want to create a C# code to first: view (ID,item_name, date_time_added) column in datagridview then calculate (date_time_NOW - date_time_added) and view the result in a new column(named: expire's in:) in same datagridview...
Note: result would count day's remaining before expiring
what i've tried so far:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Expire's in:", typeof(int)));
int countrow = dataGridView1.RowCount;
for (int i = 0; i < countrow; i++)
{
string dateAsString = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
DateTime.TryParseExact(dateAsString , "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out DateTime dateAsString);
dateTimePicker3.Text = dateAsString;
DateTime expire_date = dateTimePicker3.Value;
TimeSpan span = expire_date - DateTime.Now;
int days = span.Days;
dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value = days;
}
Note:Code Updated...
Any help will be greatly appreciated..
I will assume the “ExpireDate” field returned from the sql query is a DateTime object. If this is the case then it would appear that converting the “date” to a string is unnecessary. Example, given a “future” date, then the difference between todays date and the “future” date can be accomplished as…
TimeSpan dif = futureDate.Subtract(DateTime.Now);
Using a DataTable proffers the ability to use an Expression column, however, I do not think this will work with dates and times. Fortunately, this should not be difficult to implement if the grids DataSource is a DataTable. Using a “Class” would be another option. This example uses a DataTable as a DataSource to the grid.
Given this, to make things simple it would appear that a method that takes a DataRow from the data table and adds this TimeSpan difference may come in handy. It may look something like below…
private void SetDifCol(DataRow row) {
TimeSpan dif = ((DateTime)row["ExpireDate"]).Subtract(DateTime.Now);
row["TimeToExpire"] = dif.Days + " days " + dif.Hours + " hours " + dif.Minutes + " minutes";
}
Given that the DataTable has already been filled with the data… the code is going to have to “ADD” this difference column, then loop through each row and calculate the difference between the dates. Therefore, a small method that simply adds this column may look something like below…
private void AddDifferenceColumn(DataTable dt) {
dt.Columns.Add("TimeToExpire", typeof(string));
}
Next is the loop through all the rows in the DataTable and simply call the SetDifCol method on each row.
private void CalculateDateDif(DataTable dt) {
foreach (DataRow row in dt.Rows) {
SetDifCol(row);
}
}
This will work as expected when the data is loaded, however, what if the user “changes” one of the “ExpireDate” values in the grid? In this case, we would need to wire up one of the grids cell change events. Specifically the grids CellValueChanged event. This event will call the SetDifCol method if the “ExpireDate” value changes in that row…
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "ExpireDate") {
if (e.RowIndex >= 0 && dataGridView1.Rows[e.RowIndex].Cells["ExpireDate"].Value != null) {
DataRowView row = (DataRowView)dataGridView1.Rows[e.RowIndex].DataBoundItem;
SetDifCol(row.Row);
}
}
}
Putting this all together may look something like below…
DataTable GridTable;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
GridTable = GetTable();
FillTable(GridTable);
AddDifferenceColumn(GridTable);
CalculateDateDif(GridTable);
dataGridView1.DataSource = GridTable;
dataGridView1.Columns[3].Width = 180;
}
private DataTable GetTable() {
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("ExpireDate", typeof(DateTime));
return dt;
}
private void AddDifferenceColumn(DataTable dt) {
dt.Columns.Add("TimeToExpire", typeof(string));
}
private void FillTable(DataTable dt) {
dt.Rows.Add("ID1", "Name1", new DateTime(2019, 12, 31));
dt.Rows.Add("ID2", "Name2", new DateTime(2019, 8, 31));
dt.Rows.Add("ID3", "Name3", new DateTime(2019, 4, 30));
dt.Rows.Add("ID4", "Name4", new DateTime(2019, 1, 31));
dt.Rows.Add("ID5", "Name5", new DateTime(2019, 4, 12, 21, 38, 00));
}
private void CalculateDateDif(DataTable dt) {
foreach (DataRow row in dt.Rows) {
SetDifCol(row);
}
}
private void SetDifCol(DataRow row) {
TimeSpan dif = ((DateTime)row["ExpireDate"]).Subtract(DateTime.Now);
row["TimeToExpire"] = dif.Days + " days " + dif.Hours + " hours " + dif.Minutes + " minutes";
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "ExpireDate") {
if (e.RowIndex >= 0 && dataGridView1.Rows[e.RowIndex].Cells["ExpireDate"].Value != null) {
DataRowView row = (DataRowView)dataGridView1.Rows[e.RowIndex].DataBoundItem;
SetDifCol(row.Row);
}
}
}
I hope this helps.
EDIT:
to change column type from string to int to sort numerically.
In reference to the extra question you posted, you commented that ”i want to calculate according to what is inside my db Table” … There is no code in this question or the other question that shows a data base. How are you getting the data to begin with?
It appears in this question that there IS a NEW DataTable dt and a column is added to it, however, it is NEVER used. The loop in the code simply adds the difference column to the “GRID” NOT the DataTable. My answer “adds” the diffence column to the DataTable (which you should do). I recommend you show how you are getting the data from the data base.
In reference to sorting the column, you have already noticed that strings that are numbers will not sort properly numerically. This is because they are string… solution… make them ints. Using my answer, two changes are need for this. First the creation of the column needs to be an int type…
private void AddDifferenceColumn(DataTable dt) {
dt.Columns.Add("TimeToExpire", typeof(int));
}
Second a change is needed in the SetDifCol method. Since you only want the days difference and any values less than zero should show as zero (0), then the following changes should accommodate this requirement.
private void SetDifCol(DataRow row) {
TimeSpan dif = ((DateTime)row["ExpireDate"]).Subtract(DateTime.Now);
if (dif.Days >= 0) {
row["TimeToExpire"] = dif.Days;
}
else {
row["TimeToExpire"] = 0;
}
}
These two changes should sort the column numerically as expected.
Lastly, it should be clear, that IF you want this “difference” column to be reflected in the database… then YOU will have to add the difference column to the database table, THEN, you will need to issue an update command to the database table.
From what I see you try to put a string into the DateTime value here:
DateTime str;
str=dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
If you want to parse string to DateTime the code should look like this:
string dateAsString = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value.ToString();
DateTime.TryParseExact(dateAsString, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out DateTime dateAsDateTime);
Then you can substract that date from DateTime.Now:
TimeSpan span = dateAsDateTime - DateTime.Now;
And finally extract the days from the span:
int days = span.Days;
OR just do it all in one line:
int days = (DateTime.Now - dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[3].Value).Days;

Dataview rowfilter invalid datetime string

I'm struggling with the following. I have a DataTable with dates in it. Dates that can contain several time rows. Now I want to click on a date on the calender and it will put the times records from that day in a listbox so I can then select the time.
I'm stuck and googled around but I'm blind at this point.
The datatable output in gridview:
Datum
-----------------
22-09-14 13:05:00
22-09-14 13:05:18
23-09-14 13:05:36
23-09-14 13:05:54
23-09-14 13:06:12
21-09-14 14:00:01
21-09-14 15:00:01
21-09-14 16:00:01
21-09-14 17:00:01
The code in the calander SelectionChanged event:
// Create datatable
DataTable dt = new DataTable();
string FileName = "C:\\ProjectName\\data\\data.dat";
var lines = File.ReadAllLines(FileName);
// Make date column
dt.Columns.Add("Datum", typeof(DateTime));
// add rows
for (int i = 2; i < lines.Count(); i++)
{
DataRow dr = dt.NewRow();
string[] values = lines[i].Split(new char[] { ',' }).Select(x => x.Replace("\"", "")).ToArray();
for (int j = 0; j < values.Count() && j < 1; j++)
dr[j] = values[j];
dt.Rows.Add(dr);
}
// Convert selected date
string currentDate = e.SelectedDates.Count - 1 >= 0 ? e.SelectedDates[e.SelectedDates.Count - 1].Date.ToString("dd-MM-yyyy") : "none";
// Print selected date to see format
Label1.Text = currentDate;
// Create dataview and apply filter
DataView dv = new DataView(dt);
dv.RowFilter = "Datum = #" + currentDate + "#";
dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
dv.Sort = "Datum DESC";
// dataview to listbox
lbSource.DataSource = dv;
lbSource.DataTextFormatString = "{0:dd-MM-yyyy HH:mm}";
lbSource.DataTextField = "Datum";
lbSource.DataValueField = "Datum";
lbSource.DataBind();
The dv filter gives me:
String was not recognized as a valid DateTime.
Debugging stops with the following line and gives the "String was not recognized as a valid DateTime.":
dv.RowFilter = "Datum = #" + currentDate + "#";
currentdate comes with the correct date: 22-09-2014 (dd-MM-yyyy)
If someone could help me out, thanks a lot :)
Basically the .rowfilter compare takes the date as Mm/dd/yyyy as comparison. It will see 20/10/2018 as an invalid date. It will work for day being lower than 12.
So in your datatable, the date is stored as dd/mm/yyyy.
Unless you change the date storage format to mm/dd/yyyy, I don't think you can rowsfilter.

get datetime value from datagridview cells and change it to persian date at run time

I have a problem in DataGridView .
I fill DataGridView with a datable .
there are two columns that get date time value in DataGridView I want to change this value to Persian Date at run time what should I do?
You could fill another DataTable by using PersianCalendar:
DataTable table = new DataTable();
table.Columns.Add("Persian date 1");
table.Columns.Add("Persian date 2");
var persCal = new System.Globalization.PersianCalendar();
foreach(DataRow row in dataTable1.Rows)
{
DateTime dt1 = row.Field<DateTime>(0);
DateTime dt2 = row.Field<DateTime>(1);
int year1 = persCal.GetYear(dt1);
int month1 = persCal.GetMonth(dt1);
int day1 = persCal.GetDayOfMonth(dt1);
int year2 = persCal.GetYear(dt2);
int month2 = persCal.GetMonth(dt2);
int day2 = persCal.GetDayOfMonth(dt2);
string persDate1 = string.Format("{0}-{1}-{2}", year1, month1, day1);
string persDate2 = string.Format("{0}-{1}-{2}", year2, month2, day2);
table.Rows.Add(persDate1, persDate2);
}
Then use this as datasource.

Categories