2013-08-06 8 views
5

Questo è il mio codice per Datagridview Paging, perché sto ottenendo questo ArgumentException non gestito Child list for field Questions cannot be created. A questa riga di codice this.dataGridView1.DataSource = GetCurrentRecords(1, con);. Dove sto andando stortoGetting ArgumentException => Elenco child per campo Domande non possono essere create

public partial class ShowEngClgList : Form 
    { 
     OleDbConnection con = null; 
     private OleDbCommand cmd1; 
     private OleDbCommand cmd2; 
     private OleDbDataAdapter adp1 = null; 
     DataSet ds; 

     private int PageSize = 10; 
     private int CurrentPageIndex = 1; 
     private int TotalPage = 0; 
     public ShowEngClgList() 
     { 
      InitializeComponent(); 
      try 
      { 
       con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb"); 
       con.Open(); 
      } 
      catch (Exception err) 
      { 

      } 
      cmd1 = new OleDbCommand("Select * from Questions order by QID", con); 
      ds = new DataSet(); 
      adp1 = new OleDbDataAdapter(cmd1); 
      adp1.Fill(ds, "Questions"); 
      dataGridView1.DataSource = ds; 
      dataGridView1.DataMember = "Questions"; 

      // WORK IN PAGING FOR DATAGRIDVIEW 
      // Get total count of the pages; 
      this.CalculateTotalPages(); 
      this.dataGridView1.ReadOnly = true; 
      // Load the first page of data; 
      this.dataGridView1.DataSource = GetCurrentRecords(1, con);//Getting exception at this line 

     } 
     private void ShowEngClgList_Load(object sender, EventArgs e) 
     { 

     } 
     private void CalculateTotalPages() 
     { 
      int rowCount = ds.Tables["Questions"].Rows.Count; 
      this.TotalPage = rowCount/PageSize; 
      if (rowCount % PageSize > 0) // if remainder is more than zero 
      { 
       this.TotalPage += 1; 
      } 
     } 
     private DataTable GetCurrentRecords(int page, OleDbConnection con) 
     { 
      DataTable dt = new DataTable(); 

      if (page == 1) 
      { 
       cmd2 = new OleDbCommand("Select TOP " + PageSize + " * from Questions ORDER BY QID", con); 
      } 
      else 
      { 
       int PreviouspageLimit = (page - 1) * PageSize; 

       cmd2 = new OleDbCommand("Select TOP " + PageSize + 
        " * from Questions " + 
        "WHERE QID NOT IN " + 
       "(Select TOP " + PreviouspageLimit + " QID from Questions ORDER BY QID) ", con); // + 
       //"order by customerid", con); 
      } 
      try 
      { 
       // con.Open(); 
       this.adp1.SelectCommand = cmd2; 
       this.adp1.Fill(dt); 
      } 
      finally 
      { 
       con.Close(); 
      } 
      return dt; 
     } 
} 

grazie In anticipo per qualsiasi aiuto.

+0

Posta l'eccezione si ottiene un buon aiuto! :) – Younes

+0

L'ho postato, questa è l'eccezione che sto ricevendo 'Elenco bambini per il campo Domande non possono essere creati., Non sto ottenendo nulla in oggetto' dt', ma non mi mostra alcun errore – Durga

+0

Hmm. .. catturare QUALSIASI eccezione senza trattamento è una pessima pratica. – RoadBump

risposta

3

Quando si esegue il binding alla raccolta dati personalizzata in DataGridView tramite il set di dati, è sufficiente impostare solo l'origine dati, non è necessario impostare il datamember. Per questo motivo la rimozione di questa riga di codice risolve il problema e il codice funziona correttamente.

dataGridView1.DataMember = "EngColeges"; 
Problemi correlati