0
votes

I have a form using dataGridView and CellFormatting. One of the columns in my dataGridView is a "Timer" which should basically show how long that row has been in my dataGridView.

What I have so far (referencing How do I display a digital timer (StopWatch) in datagridview column dynamically c#?)

        public Form1()
    {
        InitializeComponent();
        FillData();
        dataGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.cell_formatting);
        this.Controls.Add(dataGridView);
        start_timer();    

    }

    void start_timer()
    {
       System.Windows.Forms.Timer clock = new System.Windows.Forms.Timer();
        clock.Interval = 1000;
        clock.Tick += new EventHandler(clock_tick);
        clock.Enabled = true;           
    }

    void clock_tick(object sender, EventArgs e)
    {
        foreach(DataGridViewRow row in dataGridView.Rows)
        {
           //????
        }
    }

The difference between the aforementioned thread and my situation is I'm using DataGridView whereas... I'm not sure what the other user was using. I don't seem to have access to "DataRow" like he does. When I try to use DataRow in my foreach loop, I get:

Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.Data.DataRow'.

I could try checking each row for a certain value that I manually insert as a flag, then set that row[column] = DateTime.Now (with some math to determine length that it's been in there..)

Any advice would be appreciated!

@Reza Aghaei fixed it. Simply updating each object in my datasource then datasource.ResetBindings() each tick.

Thanks!

1
The other answer is using DataTable and updates 'DurationColumn' column of DataRow in Tick event of the timer. You can update the property of your data model class. - Reza Aghaei
I'm not sure what that means. What is my "data model class"? - MrDysprosium
For example if you have a Record class, containing ElapsedTime property, you can change it. - Reza Aghaei
Oh.... Wow, I think I get it. One sec. - MrDysprosium
foreach(Record record in RecordList){ ...} - Reza Aghaei

1 Answers

0
votes

@Reza Aghaei has the answer:

        void clock_tick(object sender, EventArgs e)
    {
        Console.WriteLine("Tick");
        foreach(Record record in record_list)
        {
            record.EvtTimeString = DateTime.Now.ToString();
        }
        record_list.ResetBindings();
    }