Get time span for each row in datagridview in c #

So, I make a small agenda, and I insert the to-do list into the database and populate it DataGridViewwith DateTime.Now, when I saved it. If I forget to do something from this list, it will display a notification and say that I forgot to do it.

I want to use a TimeSpanmaximum of hours, and it will only display a notification if it passes 2 hours.

This is the code that I have at the moment:

private void GetSpan()
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        TimeSpan span = (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

        String.Format("{0} hours, {1} minutes, {2} seconds",
            span.Hours, span.Minutes, span.Seconds);
    }
}

The problem with this code is that it gives me only one result, and not all of them from each line DataGridView. So how can I convert it to an array and get each result?

+4
3

, , List .

private List<String> GetSpans()
{
    var timeSpanList = new List<string>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        TimeSpan span = (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

        timeSpanList.Add(String.Format("{0} hours, {1} minutes, {2} seconds",
            span.Hours, span.Minutes, span.Seconds));
    }
    return timeSpanList;
}

TimeSpans, . List<TimeSpan> , .

, , Dictionary. , ; TimeSpan .

FMI:

+2

System.Linq:

using System.Linq;

:

IEnumerable<TimeSpan> timeSpans = dataGridView1.Rows.Select(row => (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

IEnumerable, . foreach -loop Array (timespans.ToArray()) (timespans.ToList()).

:

private IEnumerable<TimeSpan> GetSpans()
    => dataGridView1.Rows.Select(row => (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

:

private IEnumerable<TimeSpan> GetSpans()
{
    dataGridView1.Rows.Select(row => (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));
}

:

foreach (TimeSpan span in this.GetSpans())
{
    string formattedTimeSpan = String.Format("{0} hours, {1} minutes, {2} seconds",
        span.Hours, span.Minutes, span.Seconds);

    doSomething(formattedTimeSpan);
}
+3

I would suggest creating a list with all the time frames so that you can iterate or select (using linq) all the elements you want to have.

// Make returnvalue list of timespans
private List<TimeSpan> GetSpan()
    {
        List<TimeSpan> allSpans = new List<TimeSpan>();

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            TimeSpan span = (DateTime.Now - Convert.ToDateTime(row.Cells[2].Value));

            String.Format("{0} hours, {1} minutes, {2} seconds",
                span.Hours, span.Minutes, span.Seconds);
            // Add timespans to list
            allSpans.Add(span);
        }

        // return list
        return allSpans;
    }

Now you can iterate over the list in another function by calling

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach(var time in GetSpan())
        {
            // doSomething with time
        }
    }
+1
source

Source: https://habr.com/ru/post/1683354/


All Articles