简体   繁体   中英

Get timespan of every row in datagridview in C#

So I doing a small agenda and I will insert the to-do list into a database and populate it in the DataGridView along with the DateTime.Now when I saved it. If I forget to do something of that list it will display a notification and tell me that I forget to do that.

I want to use a TimeSpan of a maximum of hours and it will only display the notification if it passes the 2 hours long.

This is the code 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 of this code is that it's only gives me one result and not all of them from each row of the DataGridView . So how can I convert it into an array and get each result?

First of all you need to add the using for System.Linq :

using System.Linq;

Then you can use this small snippet:

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

You can use an IEnumerable eg in an foreach -loop or convert it to an Array ( timespans.ToArray() ) or a List ( timespans.ToList() ).

Your method could look like this:

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

Or like this:

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

And you can use it like this:

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

    doSomething(formattedTimeSpan);
}

While you're on the right track looking to use an array, a List would accomplish the same thing and be easier to work with.

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;
}

This will give you a whole list of TimeSpans, one for each row in the data grid. Depending on your application, you may want to return a List<TimeSpan> instead of a formatted list of strings, but that's up to you.

Alternately, another useful collection type that may help you is Dictionary . If you have a unique ID for each row, you could store the results of this function in a Dictionary instead of List; that'd let you quickly look up any given row's TimeSpan by that row's ID later on.

FMI:

I'd suggest creating a list with all timespans, so you can iterate over or select (with linq) all the items you wish 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 through the list in another function by calling

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM