2013-08-06 19 views
5

これはDatagridviewページングのコードです。なぜこのArgumentExceptionが手元に届かないのですか。Child list for field Questions cannot be created.このコード行でthis.dataGridView1.DataSource = GetCurrentRecords(1, con);です。どこが間違っていますかGetting ArgumentException =>フィールドの子リスト質問を作成できません

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; 
     } 
} 

ありがとうございました。

+0

あなたが良い助けを得ることができます!例外を投稿してください。 :) – Younes

+0

私はそれを掲示しました、これは、 'フィールドの子リストの質問を作成することはできません。 '、私は' dt'オブジェクトに何も取得していませんが、例外を表示していますが、私にエラーが表示されません – Durga

+0

うー。 ..処理せずに例外をキャッチすることは非常に悪い習慣です。 – RoadBump

答えて

3

データセットを使用してDataGridViewにカスタムデータコレクションをバインドする場合は、データソースのみを設定するだけで、datamemberを設定する必要はありません。 このコード行を削除することで問題が解決され、コードが正常に動作しています。

dataGridView1.DataMember = "EngColeges"; 
関連する問題