The last line of this code is supposed to fill in a row with 3 columns in a datagridview every time the variable "temperature" and "umidita" are shown in the relevant text boxes.
These data comes from the serial port and get updated every x seconds.
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 });
}
}
The code is working but I get 3 rows at time filled in with the same data.
I am not familiar with datagridview and I do not understand why I am getting such behavior. What do I need to change to get one row at time filled in with time, "temperatura" and "umidita"?
////you can check if you already inserted a row in this time
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 });
}
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.