使用BindingNavigator而不使用BindingSource

Bil*_*l N 3 c# winforms

我有一个应用程序,在数据库中完成分页.也就是说,检索项目列表的调用包括页码,页面大小,并且仅返回页面的数据.例如:

ItemCollection items = ListAllItems(1, 20); // page 1, show 20 items per page
Run Code Online (Sandbox Code Playgroud)

ItemCollection包含一个PagingUtil属性,该属性是一个包含支持分页但不检索所有记录的属性的类.

public class PagingUtil
{
    public int StartRow { get; private set; }

    public int EndRow { get; private set; }

    public int TotalPages { get; private set; }

    public bool HasPrevPage { get; private set; }

    public bool HasNextPage { get; private set; }

    public int TotalCount { get; private set; }

    private PagingUtil() {}

    public PagingUtil(int pageNumber, int visiblePerPage, int totalCount) 
    {
            ... logic for setting property values here ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想在Windows窗体应用程序中使用BindingNavigator控件 UI,而不必指定BindingSource.

问题是,如果设置了BindingSource,BindingNavigator将仅渲染为启用.在设计器和代码中将Enabled属性设置为true不受尊重,我似乎无法找到变通方法或替代库存控制.

是否可以这样使用BindingNavigator?如果需要,我可以创建一个自定义分页控件,但如果我不需要,我宁愿不这样做.

edd*_*dwo 6

我今天尝试做类似的事情,最后发明了一些数据对象供BindingNavigator管理,然后代表数据库中实际数据的页面.BindingSource可以被赋予IListSource作为其DataSource,然后它将从中绘制要绑定的数据列表.

class PageList : System.ComponentModel.IListSource
{
    private const int itempagesize = 250;
    private long totalitems;

    public PageList(string tablename, long totalrecords)
    {
        this.TableName = tablename;
        totalitems = totalrecords;
    }

    public bool ContainsListCollection { get; protected set; }

    public System.Collections.IList GetList()
    {
        List<ItemPage> pages = new List<ItemPage>();
        int totalPages = (int)Math.Ceiling((double)totalitems / (double)itempagesize);
        pages.AddRange(Enumerable.Range(0, totalPages).Select(
            pageidx => new ItemPage(itempagesize, pageidx * itempagesize)));
        return pages;
    }

    public string TableName { get; protected set; }


    public class ItemPage
    {
        public ItemPage(int limit, int offset)
        {
            this.Limit = limit;
            this.Offset = offset;
        }

        public readonly int Limit;
        public readonly int Offset;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我有一个绑定到BindingSource的BindingNavigator,当我想设置可用的数据页面总数时,我只需要:

bsDataPages.DataSource = new PageList(tableName, recordCount);
Run Code Online (Sandbox Code Playgroud)

然后在使用导航器时触发绑定源事件处理程序

    private void bsDataPages_CurrentChanged(object sender, EventArgs e)
    {
        PageList list = bsDataPages.DataSource as PageList;
        PageList.ItemPage page = bsDataPages.Current as PageList.ItemPage;
        var items = m_datastore.GetTableItems(m_conn, 
            list.TableName,page.Limit,page.Offset);
    }
Run Code Online (Sandbox Code Playgroud)

我可以从数据库中获取该页面的数据.