【问题标题】:Custom Datagridview Paging自定义 Datagridview 分页
【发布时间】:2021-02-23 13:41:50
【问题描述】:

我正在尝试进行 Datagridview 分页,我已经执行了分页并且工作正常,问题是当我尝试从数据库中搜索记录时,问题是 Datagridview 没有更改并且从数据库未显示,每次搜索的页数不断增加。

请观看此视频: https://www.youtube.com/watch?v=Ina0OlZagSo&ab_channel=RabeeQabaha

这是我的自定义 Datagridview 类:

class GV_Paging : Guna.UI2.WinForms.Guna2DataGridView
{
    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];
    }
}

这就是我将数据填充到 Datagridview 的方式:

 GV.PageSize = Convert.ToInt32(Math.Floor(Convert.ToDecimal(GV.Height / GV.RowTemplate.Height))) - 1;
 DT = DBConn.ExecuteDataTable("select_all_materials", CommandType.StoredProcedure);
 GV.SetPagedDataSource(DT, bindingNavigator1);

这是从数据库中搜索数据的代码(处理文本框文本更改):

 GV.PageSize = Convert.ToInt32(Math.Floor(Convert.ToDecimal(GV.Height / GV.RowTemplate.Height))) - 1;
 DT = DBConn.ExecuteDataTable("search_materials_by_name", CommandType.StoredProcedure, new 
 SqlParameter[] { new SqlParameter("@name", Material_name_txt.Text.Trim()) });
 GV.SetPagedDataSource(DT, bindingNavigator1);

【问题讨论】:

    标签: c# winforms datagridview paging


    【解决方案1】:

    在处理之后我能够解决问题,这里是工人阶级:

    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)
    {
        if (dataTable == null || bnav == null)
        {
            return;
        }
    
        DataTable dt = null;
        bs = new BindingSource();
        tables = new BindingList<DataTable>();
        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)
    {
        try
        {
            this.DataSource = tables[bs.Position];
        }
        catch (Exception ex)
        {
    
            MessageBox.Show(ex.Message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2016-11-23
      • 2021-04-20
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多