2013-05-21 2 views
1

ListView에 DataTable이 인쇄되어 있지만 정상적으로 작동했지만 일부 시점에서이 오류가 trhowing되기 시작했습니다.C# DataTable 오류

사용자는 사용자의 마무리는 MainForm이 actualizarFormulario() 메서드를 호출 표시 될 때, 데이터베이스에 삽입 후, 윈폼을 채우고, 그래서 ListView는 새로운 데이터로 가득 :이 프로젝트에 대한 해결 방법입니다.

EDIT

이 오류의 행 156 item.SubItems.Add(row[1].ToString());이지만 또한 그 날의 foreach 내부 (153) ... (155) 모두를 제공한다.

21-05 17:00> 예외 : 예외 : System.InvalidOperationException Mensaje : 컬렉션이 수정되었습니다. 열거 연산이 실행되지 않을 수 있습니다. 오리겐 : System.Data 스택 트레이스 : Operaciones_Diversas.Principal.actualizarFormulario()에서 System.Data.RBTree`1.RBTreeEnumerator.MoveNext()에서 C에서 : \ 문서 및 설정 \ 사용자 선호도 \ 잘못 documentos \ 비주얼 스튜디오 2010 \ 프로젝트 \ Operaciones Diversas \ Operaciones Diversas \ Principal.cs : 라인 156


데이터를 기입하기위한 코드는

private void actualizarFormulario() 
{ 
    try 
    { 
     listaLotes.Items.Clear(); 
     foreach (DataRow row in Consultas.listadoLotes().Rows) 
     { 
      ListViewItem item = new ListViewItem(row[0].ToString()); 
      item.SubItems.Add(row[1].ToString()); 
      item.SubItems.Add(Convert.ToDecimal(row[2].ToString().Substring(0, row[2].ToString().Length - 2) + "," + row[2].ToString().Substring(row[2].ToString().Length - 2, 2)).ToString("N2", Cultures.Spain)); 
      item.SubItems.Add(row[3].ToString()); 
      item.SubItems.Add(row[4].ToString()); 
      listaLotes.Items.Add(item); 
     } 
    } 
    catch (Exception ex) { Logger.log(ex); } 
} 

    public static DataTable listadoLotes() 
    { 
     try 
     { 
      SelectBD sel = new SelectBD(Program.ConexBD, 
      "SELECT referencia, tipo, total_lote, COUNT(Documentos.id) as Documentos, cuenta FROM Lotes" 
      + " LEFT JOIN Documentos" 
      + " ON Lotes.referencia = Documentos.ref_lote" 
      + " WHERE Lotes.fecha_creacion='" + valoresGenerales.dateHoy + "'" 
      + " GROUP BY Lotes.referencia, Lotes.tipo, Lotes.total_lote, Lotes.cuenta" 
      + " ORDER BY Lotes.tipo" 
      ); 
      return sel.DataTable; 
     } 
     catch (Exception ex) 
     { 
      Logger.log(ex); 
      return new DataTable(); 
     } 
    } 

인 ... for 루프를 사용하여 편집 2

, 내 프로그램의 속도를 증가하고, 사용자가 모든 것을 빠른 상호 작용을 할 필요가 있기 때문에,이 방법이 될 수 없습니다

for (int i = 0; i < Consultas.listadoLotes().Rows.Count; i++) 
{ 
    ListViewItem item = new ListViewItem(Consultas.listadoLotes().Rows[i]["referencia"].ToString()); 
    item.SubItems.Add(Consultas.listadoLotes().Rows[i]["tipo"].ToString()); 
    item.SubItems.Add(Convert.ToDecimal(Consultas.listadoLotes().Rows[i]["total_lote"].ToString() 
     .Substring(0, Consultas.listadoLotes().Rows[i]["total_lote"].ToString().Length - 2) 
     + "," 
     + Consultas.listadoLotes().Rows[i]["total_lote"].ToString() 
     .Substring(Consultas.listadoLotes().Rows[i]["total_lote"].ToString().Length - 2, 2)).ToString("N2", Cultures.Spain)); 
    item.SubItems.Add(Consultas.listadoLotes().Rows[i]["Documentos"].ToString()); 
    item.SubItems.Add(Consultas.listadoLotes().Rows[i]["cuenta"].ToString()); 
    listaLotes.Items.Add(item); 
} 

EDIT 3 당신이 열거를 통해 반복하고 같은 작업 코드

 listaLotes.Items.Clear(); 
     DataTable tabla = Consultas.listadoLotes(); 
     for (int i = 0; i < tabla.Rows.Count; i++) 
     { 
      ListViewItem item = new ListViewItem(); 
      item.SubItems.Add(tabla.Rows[i]["referencia"].ToString()); 
      item.SubItems.Add(tabla.Rows[i]["tipo"].ToString()); 
      item.SubItems.Add(Convert.ToDecimal(tabla.Rows[i]["total_lote"].ToString() 
       .Substring(0, tabla.Rows[i]["total_lote"].ToString().Length - 2) 
       + "," 
       + tabla.Rows[i]["total_lote"].ToString() 
       .Substring(tabla.Rows[i]["total_lote"].ToString().Length - 2, 2)).ToString("N2", Cultures.Spain)); 
      item.SubItems.Add(tabla.Rows[i]["Documentos"].ToString()); 
      item.SubItems.Add(tabla.Rows[i]["cuenta"].ToString()); 
      listaLotes.Items.Add(item); 
     } 
+0

foreach에서 컬렉션을 수정할 수 없습니다. normal for 루프를 사용해야합니다. –

+0

하지만 내가 말했듯이, 모든 것이 완벽하게 작동했다. 오류는 몇 분 전에 무작위로 시작되었다 ... –

+1

이 줄'listaLotes.Items.Add (item);'에 주석 처리하고 모든 것이 다시 작동 할 것이다 –

답변

6

당신은 컬렉션을 수정하고 있습니다. foreach 루프 대신에 for 루프를 사용하십시오.

+0

for 루프를 사용하면 프로그램이 이전보다 훨씬 느려집니다 : S –

+0

하지만 적어도 오류없이 작동합니다. –

+0

...예.하지만 MainForm을로드하는 데 8 초가 걸리는 것은 실용적이지 않습니다. –