我正在尝试使用计时器。如果经过,当前时间DateTime.Now
应显示在我的文本框"注释"中,例如提醒。
我的计时器正在工作,但前提是我单击datagridview
.如果我不单击数据网格,则不会发生任何反应。如果我单击显示笔记并删除行?
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();
}
更改单元格中的值或以某种方式操作行不一定会导致显示的内容发生变化。通常,数据网格绑定到 List,其中 T 是实现 INotifyPropertyChanged 的某个对象。这样做是因为 INotifyPropertyChanged 通知网格某些内容已更改,并且网格需要更新自身以反映该更改。
在您提供的代码中,我建议在计时器经过方法结束时添加对 datagridview 刷新方法的调用。
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
}
已解决:(我的问题已编辑。
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();
}