将LINQ结果显示到数据网格视图中

时间:2012-11-30 20:15:07

标签: c# sql winforms list datagridview

当我尝试在数据网格视图上显示linQ结果时,我的内存出错了 Data Grid View的行数是否有限制?  我想显示41299440

这是错误消息

A first chance exception of type 'System.OutOfMemoryException'
occurred in System.Drawing.dll

这是linQ代码

var sets =
    from a in patient
    from b in patient
    from c in patient
    from d in patient
    from l in patient
    where a.VisitNum < b.VisitNum 
          && b.VisitNum < c.VisitNum 
          && c.VisitNum < d.VisitNum 
          && d.VisitNum < l.VisitNum

    select new { a, b, c, d, l };

以及我如何显示结果

object[] buffer = new object[5];
List<DataGridViewRow> rows = new List<DataGridViewRow>();
foreach (var custObj in sets)
{
    buffer[0] = custObj.a.VisitNum;
    buffer[1] = custObj.a.ID;
    buffer[2] = patientname;
    buffer[3] = custObj.a.date;
    buffer[4] = description(Convert.ToInt32(custObj.a.RaId));
    rows.Add(new DataGridViewRow());
    rows[rows.Count - 1].CreateCells(dataGridView1, buffer);

    buffer[0] = custObj.b.VisitNum;
    buffer[1] = custObj.b.ID;
    buffer[2] = patientname;
    buffer[3] = custObj.b.date;
    buffer[4] = description(Convert.ToInt32(custObj.b.RaId));
    rows.Add(new DataGridViewRow());
    rows[rows.Count - 1].CreateCells(dataGridView1, buffer);

    buffer[0] = custObj.c.VisitNum;
    buffer[1] = custObj.c.ID;
    buffer[2] = patientname;
    buffer[3] = custObj.c.date;
    buffer[4] = description(Convert.ToInt32(custObj.c.RaId));
    rows.Add(new DataGridViewRow());
    rows[rows.Count - 1].CreateCells(dataGridView1, buffer);

    buffer[0] = custObj.d.VisitNum;
    buffer[1] = custObj.d.ID;
    buffer[2] = patientname;
    buffer[3] = custObj.d.date;
    buffer[4] = description(Convert.ToInt32(custObj.d.RaId));
    rows.Add(new DataGridViewRow());
    rows[rows.Count - 1].CreateCells(dataGridView1, buffer);

    buffer[0] = custObj.l.VisitNum;
    buffer[1] = custObj.l.ID;
    buffer[2] = patientname;
    buffer[3] = custObj.l.date;
    buffer[4] = description(Convert.ToInt32(custObj.l.RaId));
    rows.Add(new DataGridViewRow());
    rows[rows.Count - 1].CreateCells(dataGridView1, buffer);
    rows.Add(new DataGridViewRow());                           
}
dataGridView1.Rows.AddRange(rows.ToArray());

有什么建议吗?

0 个答案:

没有答案