【问题标题】:Windows Forms GridView PagingWindows 窗体 GridView 分页
【发布时间】:2014-03-05 17:53:58
【问题描述】:

我的应用程序上有一个网格视图(Windows 窗体)。数据来自网络服务调用。有时数据很大,需要一段时间。所以我想添加一些功能,如页面。所以用户可以点击首页、上一页、下一页、最后一页。我知道在 asp.net 中有这样的控制。想知道 windows 窗体,是否有类似的控件可用?如果没有,我必须自己编写代码,谢谢

【问题讨论】:

    标签: c# winforms gridview


    【解决方案1】:

    当用户点击“下一页”按钮时,在事件 bindingSource1_CurrentChanged 和你的代码可以绑定记录

    将 BindingNavigator、DataGridView 和 BindingSource 拖到窗体上

    namespace PagedView
    {
        public partial class Form1 : Form
        {
            private const int totalRecords = 40;
            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)
            {
                // 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;
                }
            }
        }
    }
    

    或点击此链接

    Paging in Winform

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2020-08-21
      相关资源
      最近更新 更多