我想从一个互联网连接的服务器上检索大量数据(100000行),并在一个自定义组合框中输入数据的ID。大量数据会导致数据检索花费很长时间,这是低效的。
为了克服这个问题,我想在自定义组合框的底部添加一个分页按钮,以便根据需要显示某些数据(可能是100行)。
在C# WinForm中打开组合框时,如何在自定义组合框底部显示分页按钮?
谢谢
自定义组合框
namespace DiscountCard.View.CustomControl
{
public partial class CtechComboBox : ComboBox
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
public CtechComboBox()
{
InitializeComponent();
DoubleBuffered = true;
DropDownStyle = ComboBoxStyle.DropDownList;
DrawMode = DrawMode.OwnerDrawFixed;
Font = new Font("Consolas", 11F, FontStyle.Regular, GraphicsUnit.Point);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index < 0 || e.Index >= Items.Count)
{
return;
}
var item = Items[e.Index];
String? text = item == null ? "(null)" : item.ToString();
TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
base.OnDrawItem(e);
}
}
}
分页按钮
using CardData.Ext.Mod.Logger;
using DiscountCard.Ext.Mod.General;
namespace DiscountCard.View.CustomControl
{
public partial class CtechPagination : UserControl
{
public class Page
{
public int MinIndex { get; set; }
public int MaxIndex { get; set; }
}
public enum PaginationPosition
{
FirstPage,
PreviousPage,
NextPage,
LastPage,
CurrrentPage
}
public CtechPagination()
{
InitializeComponent();
FirstPaginationButton.Click += OnFirstPaginationButton_Click;
PrevPaginationButton.Click += OnPrevPaginationButton_Click;
NextPaginationButton.Click += OnNextPaginationButton_Click;
LastPaginationButton.Click += OnLastPaginationButton_Click;
}
public void SetInitialize(IQueryable<int> source, bool newContent, PaginationPosition selectedPagination)
{
PageSize = Common.DEFAULT_PAGE_SIZE;
TotalCount = source.Count();
if (TotalCount <= 0)
{
return;
}
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
if (TotalPages <= 0)
{
return;
}
if (newContent)
{
Pages = new List<Page>();
for (int i = 0; i < TotalPages; i++)
{
IQueryable<int> indexes = (IQueryable<int>)source.Skip(i * PageSize).Take(PageSize);
if (indexes != null)
{
Page page = new Page();
page.MinIndex = 1 + indexes.Min();
page.MaxIndex = 1 + indexes.Max();
Pages.Add(page);
}
}
}
if (Pages != null)
{
switch (selectedPagination)
{
case PaginationPosition.FirstPage:
if (newContent)
{
PageIndex = 0;
}
Page? firstPage = Pages[PageIndex];
if (firstPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", firstPage.MinIndex, firstPage.MaxIndex);
FirstPage = firstPage;
CurrentPage = firstPage;
}
break;
case PaginationPosition.PreviousPage:
if (newContent)
{
--PageIndex;
}
if (PageIndex < 0)
{
PageIndex = 0;
}
Page? prevPage = Pages[PageIndex];
if (prevPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", prevPage.MinIndex, prevPage.MaxIndex);
PreviousPage = prevPage;
CurrentPage = prevPage;
}
break;
case PaginationPosition.NextPage:
if (newContent)
{
++PageIndex;
}
if (PageIndex > Pages.Count - 1)
{
PageIndex = Pages.Count - 1;
}
Page? nextPage = Pages[PageIndex];
if (nextPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", nextPage.MinIndex, nextPage.MaxIndex);
NextPage = nextPage;
CurrentPage = nextPage;
}
break;
case PaginationPosition.LastPage:
if (newContent)
{
PageIndex = Pages.Count - 1;
}
if (PageIndex < 0)
{
PageIndex = 0;
}
Page? lastPage = Pages[PageIndex];
if (lastPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", lastPage.MinIndex, lastPage.MaxIndex);
LastPage = lastPage;
CurrentPage = lastPage;
}
break;
case PaginationPosition.CurrrentPage:
if (PageIndex < 0)
{
PageIndex = 0;
}
if (PageIndex > Pages.Count - 1)
{
PageIndex = Pages.Count - 1;
}
Page? curentPage = Pages[PageIndex];
if (curentPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", curentPage.MinIndex, curentPage.MaxIndex);
CurrentPage = curentPage;
}
break;
}
}
}
private EventHandler? _firstPaginationButtonEventHandler;
public event EventHandler FirstPaginationButton_Click
{
add
{
_firstPaginationButtonEventHandler += value;
}
remove
{
_firstPaginationButtonEventHandler -= value;
}
}
private EventHandler? _prevPaginationButtonEventHandler;
public event EventHandler PrevPaginationButton_Click
{
add
{
_prevPaginationButtonEventHandler += value;
}
remove
{
_prevPaginationButtonEventHandler -= value;
}
}
private EventHandler? _nextPaginationButtonEventHandler;
public event EventHandler NextPaginationButton_Click
{
add
{
_nextPaginationButtonEventHandler += value;
}
remove
{
_nextPaginationButtonEventHandler -= value;
}
}
private EventHandler? _lastPaginationButtonEventHandler;
public event EventHandler LastPaginationButton_Click
{
add
{
_lastPaginationButtonEventHandler += value;
}
remove
{
_lastPaginationButtonEventHandler -= value;
}
}
protected void OnFirstPaginationButton_Click(object? sender, EventArgs e)
{
try
{
EventHandler? handler = _firstPaginationButtonEventHandler;
if (handler != null)
{
if (Pages == null)
{
return;
}
if (Pages.Count == 0)
{
return;
}
PageIndex = 0;
Page? firstPage = Pages[PageIndex];
if (firstPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", firstPage.MinIndex, firstPage.MaxIndex);
FirstPage = firstPage;
CurrentPage = firstPage;
handler(sender, e);
}
}
}
catch (Exception ex)
{
ExLogger.LogException(ex, "");
}
}
protected void OnPrevPaginationButton_Click(object? sender, EventArgs e)
{
try
{
EventHandler? handler = _prevPaginationButtonEventHandler;
if (handler != null)
{
if (Pages == null)
{
return;
}
if (Pages.Count == 0)
{
return;
}
--PageIndex;
if (PageIndex < 0)
{
PageIndex = 0;
}
Page? prevPage = Pages[PageIndex];
if (prevPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", prevPage.MinIndex, prevPage.MaxIndex);
PreviousPage = prevPage;
CurrentPage = prevPage;
handler(sender, e);
}
}
}
catch (Exception ex)
{
ExLogger.LogException(ex, "");
}
}
protected void OnNextPaginationButton_Click(object? sender, EventArgs e)
{
try
{
EventHandler? handler = _nextPaginationButtonEventHandler;
if (handler != null)
{
if (Pages == null)
{
return;
}
if (Pages.Count == 0)
{
return;
}
++PageIndex;
if (PageIndex > Pages.Count - 1)
{
PageIndex = Pages.Count - 1;
}
Page? nextPage = Pages[PageIndex];
if (nextPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", nextPage.MinIndex, nextPage.MaxIndex);
NextPage = nextPage;
CurrentPage = nextPage;
handler(sender, e);
}
}
}
catch (Exception ex)
{
ExLogger.LogException(ex, "");
}
}
protected void OnLastPaginationButton_Click(object? sender, EventArgs e)
{
try
{
EventHandler? handler = _lastPaginationButtonEventHandler;
if (handler != null)
{
if (Pages == null)
{
return;
}
if (Pages.Count == 0)
{
return;
}
PageIndex = Pages.Count - 1;
Page? lastPage = Pages[PageIndex];
if (lastPage != null)
{
PagesLabel.Text = String.Format("{0:N0} of {1:N0}", lastPage.MinIndex, lastPage.MaxIndex);
LastPage = lastPage;
CurrentPage = lastPage;
handler(sender, e);
}
}
}
catch (Exception ex)
{
ExLogger.LogException(ex, "");
}
}
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public List<Page>? Pages { get; private set; }
public Page? CurrentPage { get; private set; }
public Page? FirstPage { get; private set; }
public Page? PreviousPage { get; private set; }
public Page? NextPage { get; private set; }
public Page? LastPage { get; private set; }
}
}
1条答案
按热度按时间aiqt4smr1#
产出