引发类型为“System.OutOfMemoryException”的异常。 遍历DataGridView 获取行错误

来源:互联网 发布:五轴加工中心编程软件 编辑:程序博客网 时间:2024/04/30 10:53

因为数据较大(十万条以上,有错误的行大概有3万多条),遍历DataGridView时出现内存溢出。

以前的代码如下:

List<DataRow> rows = new List<DataRow>();foreach (DataGridViewRow row in dataGridView1.Rows){DataRow DR = (row.DataBoundItem as DataRowView).Row;if (DR.RowError != ""){rows.Add(DR);}}

解决思路:首先得到有错误的行,然后再遍历。

List<DataRow> rows = new List<DataRow>();DataTable dt = dataGridView1.DataSource as DataTable;DataRow[] RS = dt.GetErrors();foreach (DataRow row in RS){if (row.RowError != ""){rows.Add(row);}}




0 0