[英]Datagridview adds up unwanted rows
每次在相关文本框中显示变量“ temperature”和“ umidita”时,该代码的最后一行应在datagridview中用3列填充一行。
这些数据来自串行端口,每x秒更新一次。
private void SetText(string text)
{
string temperatura;
string umidita;
if (this.txtOutput.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.BeginInvoke(d, new object[] { text });
}
else
{
string f = text;
char firstChar = f[0];
if (firstChar == 'T')
{
temperatura = f;
temperatura = f.Replace("T", "");
textBox1.Text = temperatura;
}
else if (firstChar == 'U')
{
umidita = f;
umidita = f.Replace("U", "");
textBox2.Text = umidita;
}
dataGridView1.Rows.Add(new string[] { DateTime.Now.ToString("HH:mm:ss"), textBox1.Text, textBox2.Text });
}
}
该代码正在运行,但是我一次获得了3行,并填充了相同的数据。
我不熟悉datagridview,也不清楚为什么会出现这种行为。 我需要更改什么才能在时间上填充“ temperatura”和“ umidita”一行?
////您可以检查这一次是否已插入行
int rowsCount = dataGridView1.Rows.Count;
string cuurentTime = DateTime.Now.ToString("HH:mm:ss");
if (cuurentTime != dataGridView1.Rows[rowsCount - 1].Cells[0].Value.ToString())
{
dataGridView1.Rows.Add(new string[] { cuurentTime, textBox1.Text, textBox2.Text });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.