2010-05-13 18 views
13

我想在窗體的datagridview中顯示每頁10條記錄,用戶必須點擊下一個按鈕才能顯示下10條記錄。 DataGridview中是否有一些屬性,或者是否需要創建自定義控件?我們如何在winform的datagridview中進行分頁

我需要做些什麼才能做到這一點。

+0

這個鏈路可以幫助http://programcall.com/9/dotnet/datagridview-paging-開始.net-winforms-cs.aspx – Mou 2015-08-04 14:14:58

回答

27

這裏有一個簡單的工作示例,其中 BindingNavigator GUI控件使用 BindingSource對象 識別分頁符,通過其DataSource設置的IListSource自定義子類。 (感謝this answer對於 的關鍵想法。)當用戶點擊「下一頁」按鈕時,BindingNavigator觸發bindingSource1_CurrentChanged,您的代碼可以獲取所需的記錄。說明:

  1. 創建一個Windows窗體應用程序
  2. 拖到形式BindingNavigator,一個DataGridView和BindingSource的
  3. 用下面的代碼替換Form1.cs中:
using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 

namespace PagedDataGridView 
{ 
    public partial class Form1 : Form 
    { 
     private const int totalRecords = 43; 
     private const int pageSize = 10; 

     public Form1() 
     { 
      InitializeComponent(); 
      dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Index" }); 
      bindingNavigator1.BindingSource = bindingSource1; 
      bindingSource1.CurrentChanged += new System.EventHandler(bindingSource1_CurrentChanged); 
      bindingSource1.DataSource = new PageOffsetList(); 
     } 

     private void bindingSource1_CurrentChanged(object sender, EventArgs e) 
     { 
      // The desired page has changed, so fetch the page of records using the "Current" offset 
      int offset = (int)bindingSource1.Current; 
      var records = new List<Record>(); 
      for (int i = offset; i < offset + pageSize && i < totalRecords; i++) 
       records.Add(new Record { Index = i }); 
      dataGridView1.DataSource = records; 
     } 

     class Record 
     { 
      public int Index { get; set; } 
     } 

     class PageOffsetList : System.ComponentModel.IListSource 
     { 
      public bool ContainsListCollection { get; protected set; } 

      public System.Collections.IList GetList() 
      { 
       // Return a list of page offsets based on "totalRecords" and "pageSize" 
       var pageOffsets = new List<int>(); 
       for (int offset = 0; offset < totalRecords; offset += pageSize) 
        pageOffsets.Add(offset); 
       return pageOffsets; 
      } 
     } 
    } 
} 
+0

@Rick Mohr:嗨!非常感謝你的代碼。這真的很有幫助。只是一個問題。我有這樣的代碼: var transactionLogCount = TransactionLogManager.GetTransactionLogCount(); 上述命令給出了總數。 var transactionLogList = new List (); transactionLogList.AddRange(TransactionLogManager.GetAllTransactionLog(INT開始索引,INT ENDINDEX);! 現在該怎麼辦分頁時我需要添加所有的時間在for循環 – 2013-09-19 12:41:14

6

這是我的解決方案:花了我近一年的時間才找到它併爲此感到自豪

public class SuperGrid : DataGridView 
    { 
     public int PageSize 
     { 
      get 
      { 
       return _pageSize; 
      } 
      set 
      { 
       _pageSize = value; 
      } 
     } 
     public int _pageSize = 10; 
     BindingSource bs = new BindingSource(); 
     BindingList<DataTable> tables = new BindingList<DataTable>(); 
     public void SetPagedDataSource(DataTable dataTable, BindingNavigator bnav) 
     { 
      DataTable dt = null; 
      int counter = 1; 
      foreach (DataRow dr in dataTable.Rows) 
      { 
       if (counter == 1) 
       { 
        dt = dataTable.Clone(); 
        tables.Add(dt); 
       } 
       dt.Rows.Add(dr.ItemArray); 
       if (PageSize < ++counter ) 
       { 
        counter = 1; 
       } 
      } 
      bnav.BindingSource = bs; 
      bs.DataSource = tables; 
      bs.PositionChanged += bs_PositionChanged; 
      bs_PositionChanged(bs, EventArgs.Empty); 
     } 
     void bs_PositionChanged(object sender, EventArgs e) 
     { 
      this.DataSource = tables[bs.Position]; 
     } 
    } 

如何使用它?將上面的代碼添加到您的項目中,將Supergrid和一個bindingnavigator控件拖到您的勝利表單中。

superGrid1.PageSize = 5; 
DataTable dt = DataProvider.ExecuteDt("select * from test order by col"); 
    superGrid1.SetPagedDataSource(dt, bindingNavigator1); 

而你得到的數據沒有太多hastle結合分頁的datagridview/

+0

大剛創建自定義控件使用它很多次你應該改變一些事情1。 - 在SetPagedDataSource的開始,你應該初始化變量bs和表:bs = new BindingSource(); tables = new BindingList (); 2.-在bs_PositionChanged()內的動作之前檢查(bs.Position> 0)。恭喜! – 2014-08-26 11:19:57

+0

你如何修改列,假設你想添加像查看,編輯和刪除列 – 2017-07-20 19:36:43

1

此問題的另一個方法:

public class PagedGrid : DataGridView 
    { 
     Paging pg; 
     SQLQuery s; 
     public void SetPagedDataSource( SQLQuery s, BindingNavigator bnav) 
     { 
      this.s = s; 
      int count = DataProvider.ExecuteCount(s.CountQuery); 
      pg = new Paging(count, 5); 
      bnav.BindingSource = pg.BindingSource; 
      pg.BindingSource.PositionChanged += new EventHandler(bs_PositionChanged); 
      //first page 
      string q = s.GetPagingQuery(pg.GetStartRowNum(1), pg.GetEndRowNum(1), true); 
      DataTable dt = DataProvider.ExecuteDt(q); 
      DataSource = dt; 
     } 

     void bs_PositionChanged(object sender, EventArgs e) 
     { 
      int pos = ((BindingSource)sender).Position + 1; 
      string q = s.GetPagingQuery(pg.GetStartRowNum(pos), pg.GetEndRowNum(pos), false); 
      DataTable dt = DataProvider.ExecuteDt(q); 
      DataSource = dt; 
     } 

     public void UpdateData() 
     { 
      DataTable dt = (DataTable)DataSource; 
      using (SqlConnection con = new SqlConnection(DataProvider.conStr)) 
      { 
       con.Open(); 
       SqlDataAdapter da = new SqlDataAdapter(s.CompleteQuery, con); 
       SqlCommandBuilder cb = new SqlCommandBuilder(da); 
       da.UpdateCommand = cb.GetUpdateCommand(); 
       da.InsertCommand = cb.GetInsertCommand(); 
       da.DeleteCommand = cb.GetDeleteCommand(); 
       da.Update(dt); 
      } 
      MessageBox.Show("The changes are committed to database!"); 
     } 
    } 


    /// <summary> 
    /// Gives functionality of next page , etc for paging. 
    /// </summary> 
    public class Paging 
    { 
     public int _totalSize = 0; 
     private int _pageSize = 0; 

     public int TotalSize 
     { 
      get 
      { 
       return _totalSize; 
      } 
      set 
      { 
       if (value <= 0) 
       { 
        throw new ArgumentException(); 
       } 
       _totalSize = value; 
      } 
     } 

     public int PageSize 
     { 
      get 
      { 
       return _pageSize; 
      } 
      set 
      { 
       if (value <= 0) 
       { 
        throw new ArgumentException(); 
       } 
       _pageSize = value; 
      } 
     } 

     public Paging(int totalSize, int pageSize) 
     { 
      this.TotalSize = totalSize; 
      this.PageSize = pageSize; 
     } 

     public int GetStartRowNum(int PageNum) 
     { 
      if (PageNum < 1) 
      { 
       throw new Exception("Page number starts at 1"); 
      } 
      if (PageNum > GetPageCount()) 
      { 
       throw new Exception("Page number starts at " + GetPageCount().ToString()); 
      } 
      return 1 + ((PageNum - 1) * _pageSize); 
     } 

     public int GetEndRowNum(int PageNum) 
     { 
      if (PageNum < 1) 
      { 
       throw new Exception("Page number starts at 1"); 
      } 
      if (PageNum > GetPageCount()) 
      { 
       throw new Exception("Page number starts at " + GetPageCount().ToString()); 
      } 
      return _pageSize + ((PageNum - 1) * _pageSize); 
     } 

     public int GetPageCount() 
     { 
      return (int)Math.Ceiling(TotalSize/(decimal)PageSize); 
     } 

     public bool IsFirstPage(int PageNum) 
     { 
      if (PageNum == 1) 
      { 
       return true; 
      } 
      return false; 
     } 

     public bool IsLastPage(int PageNum) 
     { 
      if (PageNum == GetPageCount()) 
      { 
       return true; 
      } 
      return false; 
     } 
     private int _currentPage = 1; 
     public int CurrentPage 
     { 
      get 
      { 
       return _currentPage; 
      } 
      set 
      { 
       _currentPage = value; 
      } 
     } 
     public int NextPage 
     { 
      get 
      { 
       if (CurrentPage + 1 <= GetPageCount()) 
       { 
        _currentPage = _currentPage + 1; 
       } 
       return _currentPage; 
      } 
     } 

     public int PreviousPage 
     { 
      get 
      { 
       if (_currentPage - 1 >= 1) 
       { 
        _currentPage = _currentPage - 1; 
       } 
       return _currentPage; 
      } 
     } 
     private BindingSource _bindingSource = null; 
     public BindingSource BindingSource 
     { 
      get 
      { 
       if (_bindingSource == null) 
       { 
        _bindingSource = new BindingSource(); 
        List<int> test = new List<int>(); 
        for (int i = 0; i < GetPageCount(); i++) 
        { 
         test.Add(i); 
        } 
        _bindingSource.DataSource = test; 
       } 
       return _bindingSource; 
      } 

     } 
    } 


    /// <summary> 
    /// Query Helper of Paging 
    /// </summary> 
    public class SQLQuery 
    { 

     private string IDColumn = ""; 
     private string WherePart = " 1=1 "; 
     private string FromPart = ""; 
     private string SelectPart = ""; 

     public SQLQuery(string SelectPart, string FromPart, string WherePart, string IDColumn) 
     { 
      this.IDColumn = IDColumn; 
      this.WherePart = WherePart; 
      this.FromPart = FromPart; 
      this.SelectPart = SelectPart; 

     } 

     public string CompleteQuery 
     { 
      get 
      { 
       if (WherePart.Trim().Length > 0) 
       { 
        return string.Format("Select {0} from {1} where {2} ", SelectPart, FromPart, WherePart); 
       } 
       else 
       { 
        return string.Format("Select {0} from {1} ", SelectPart, FromPart); 
       } 
      } 
     } 

     public string CountQuery 
     { 
      get 
      { 
       if (WherePart.Trim().Length > 0) 
       { 
        return string.Format("Select count(*) from {0} where {1} ", FromPart, WherePart); 
       } 
       else 
       { 
        return string.Format("Select count(*) from {0} ", FromPart); 

       } 
      } 
     } 



     public string GetPagingQuery(int fromrow, int torow, bool isSerial) 
     { 
      fromrow--; 
      if (isSerial) 
      { 
       return string.Format("{0} where {1} >= {2} and {1} <= {3}", CompleteQuery, IDColumn, fromrow, torow); 
      } 
      else 
      { 
       string select1 = ""; 
       string select2 = ""; 
       if (WherePart.Trim().Length > 0) 
       { 
        select1 = string.Format("Select top {3} {0} from {1} where {2} ", SelectPart, FromPart, WherePart, torow.ToString()); 
        select2 = string.Format("Select top {3} {0} from {1} where {2} ", SelectPart, FromPart, WherePart, fromrow.ToString()); 
       } 
       else 
       { 
        select1 = string.Format("Select top {2} {0} from {1} ", SelectPart, FromPart, torow.ToString()); 
        select2 = string.Format("Select top {2} {0} from {1} ", SelectPart, FromPart, fromrow.ToString()); 
       } 
       if (fromrow <= 1) 
       { 
        return select1; 
       } 
       else 
       { 
        return string.Format("{0} except {1} ", select1, select2); 
       } 

      } 
     } 


    } 

使用它:

private void Form1_Load(object sender, EventArgs e) 
     { 
      SQLQuery s = new SQLQuery("*", "table", "", "id"); 
      pagedGrid1.SetPagedDataSource(s, bindingNavigator1); 
     } 

注意: DataPrivier類不包含在這裏,它是一個簡單的類,可以從任何來源返回數據。

1

試試這個, 此代碼適用於OleDb,但也適用於SqlServer連接。 對DT(數據表)對象被填充有選擇的頁的行,假設頁面1(不爲0)

public DataTable getData(string sql, int pgNo, int totalRows) 
{ 
DataTable dt = null; 
     using (OleDbConnection conn = new OleDbConnection(connStr)) 
     { 
      try 
      { 
       DataSet ds; 
       conn.Open(); 
       ds = new DataSet(); 
       OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn); 
       adapter.Fill(ds, (pgNo-1)*totalRows, totalRows, "Table"); 
       conn.Close(); 
       dt = ds.Tables[0]; 
      } 
      catch (Exception ex) 
      {if (conn != null) conn.Dispose();} 
return dt; 
}