自定义Datagridview分页
Custom Datagridview Paging
我正在尝试对 Datagridview 进行分页,我已经执行了分页并且工作正常,问题是当我尝试从数据库中搜索记录时,问题是 Datagridview 没有改变并且返回了数据来自数据库的内容未显示,每次搜索的页数都在增加。
请观看此视频:
https://www.youtube.com/watch?v=Ina0OlZagSo&ab_channel=RabeeQabaha
这是我的自定义 Datagridview class:
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);
经过处理后,我能够解决问题,这是有效的 class:
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);
}
}
我正在尝试对 Datagridview 进行分页,我已经执行了分页并且工作正常,问题是当我尝试从数据库中搜索记录时,问题是 Datagridview 没有改变并且返回了数据来自数据库的内容未显示,每次搜索的页数都在增加。
请观看此视频: https://www.youtube.com/watch?v=Ina0OlZagSo&ab_channel=RabeeQabaha
这是我的自定义 Datagridview class:
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);
经过处理后,我能够解决问题,这是有效的 class:
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);
}
}