C#的DataGridView表格显示控件,非常容易导致软件卡死,
如果一直快速显示数据,前8-9条都还好,一旦行数特别大,直接导致整个软件卡死。
为了解决此问题需要重绘一下数据。使用以下方法解决,并且增加try catch也是必须的。
需要显示数据调用 ShowData_To_DataGridView 进行显示。
但是如果增加的行数特别多,仍不会实时显示,会一直增加行数,直到显示完毕所有指定的行数,期间会卡顿,但不会造成软件崩溃。
private delegate void RetryShowData(string time, string data);
public void ShowData_To_DataGridView(string time, string data)
{
try
{
//* 如果显示出错
if (DataGridView_ShowReceiveData.InvokeRequired) //*显示出错,重绘数据到窗口
{
RetryShowData c = new RetryShowData(ShowData_To_DataGridView);
this.Invoke(c, new object[] { time, data });
}
else
{
int index = this.DataGridView_ShowReceiveData.Rows.Add();
this.DataGridView_ShowReceiveData.Rows[index].Cells[0].Value = time;
this.DataGridView_ShowReceiveData.Rows[index].Cells[1].Value = data;
this.DataGridView_ShowReceiveData.FirstDisplayedScrollingRowIndex = this.DataGridView_ShowReceiveData.Rows.Count - 1;
}
}
catch { }
}
个人见解,感谢阅读。