简体   繁体   中英

Timer Elapsed with DataGridView?

I am trying to use a Timer. If elapsed, the current time DateTime.Now should show up in my textbox Note like a Reminder.

My timer is working, but only if I click on datagridview . If I don't click the datagrid, nothing happens. If I click show up notes and delete row?

System.Timers.Timer EntTimer = new System.Timers.Timer(1000);
private void Timer()
{
    EntTimer.Elapsed += EntTimer_Elapsed;
    EntTimer.Enabled = true;
    EntTimer.Start();
}
private void EntTimer_Elapsed(object Sender, System.Timers.ElapsedEventArgs e)
{
    int columnIndex = dgV.CurrentCell.ColumnIndex;
    int rowIndex = dgV.CurrentCell.RowIndex;
    var TheDate = DateTime.Now;
    var dgvDate = Convert.ToDateTime(dgV.Rows[rowIndex].Cells["Tarih"].Value);
        if (TheDate > dgvDate)
        {
            DeleteMet();
        }
     EntTimer.AutoReset = true;
}
DeleteMet()

private void DeleteMet()
{
   int rowIndex = dgV.CurrentCell.RowIndex;
   string SelectRow =dgV.Rows[rowIndex].Cells[0].Value.ToString();
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("Delete from Timer Where ID=@Id", conn);
    cmd.Parameters.Add(new OleDbParameter("@Id", SelectRow));
    if (cmd.ExecuteNonQuery() == 1)
    {
        int rowIndex = dgV.CurrentCell.RowIndex;
        string message = txtNotes.Text = dgV.Rows[rowIndex].Cells["Notes"].Value.ToString();
        MessageBox.Show(message, "Hatırlatma !!!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    }
    conn.Close();
    MainShow();
    FirstRowHL();
}

Changing the value in a cell or manipulating the row in some way will not necessarily cause what is displayed to change. Typically your datagrid is bound to a List, where T is some object that implements INotifyPropertyChanged. You do this because INotifyPropertyChanged informs the grid that something has changed and the grid needs to update itself to reflect that change.

In the code you have provided, I would suggest adding a call to the refresh method of the datagridview at the end of your timer elapsed method.

private void EntTimer_Elapsed(object Sender, System.Timers.ElapsedEventArgs e)
{
    int columnIndex = dgV.CurrentCell.ColumnIndex;
    int rowIndex = dgV.CurrentCell.RowIndex;
    var TheDate = DateTime.Now;
    var dgvDate = Convert.ToDateTime(dgV.Rows[rowIndex].Cells["Tarih"].Value);
    if (TheDate > dgvDate)
    {
        DeleteMet();
    }
    EntTimer.AutoReset = true;
    dgv.Refresh();//ADD THIS LINE
}

SOLVED: (my question edited.)

System.Timers.Timer EntTimer = new System.Timers.Timer(1000);
        private void Timer()
        {
            EntTimer.Elapsed += EntTimer_Elapsed;
            EntTimer.Enabled = true;
            EntTimer.Start();
        }
        private void EntTimer_Elapsed(object Sender, System.Timers.ElapsedEventArgs e)
        {
            int columnIndex = dgV.CurrentCell.ColumnIndex;
            int rowIndex = dgV.CurrentCell.RowIndex;
            var TheDate = DateTime.Now;
            var dgvDate = Convert.ToDateTime(dgV.Rows[rowIndex].Cells["Tarih"].Value);
                if (TheDate > dgvDate)
                {
                    DeleteMet();
                }
             EntTimer.AutoReset = true;
        }
        DeleteMet()

        private void DeleteMet()
        {
           int rowIndex = dgV.CurrentCell.RowIndex;
           string SelectRow =dgV.Rows[rowIndex].Cells[0].Value.ToString();
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("Delete from Timer Where ID=@Id", conn);
            cmd.Parameters.Add(new OleDbParameter("@Id", SelectRow));
            if (cmd.ExecuteNonQuery() == 1)
            {
                int rowIndex = dgV.CurrentCell.RowIndex;
                string message = txtNotes.Text = dgV.Rows[rowIndex].Cells["Notes"].Value.ToString();
                MessageBox.Show(message, "Hatırlatma !!!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            conn.Close();
            MainShow();
            FirstRowHL();
        }

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