轉(zhuǎn)帖|使用教程|編輯:龔雪|2020-12-25 13:38:16.250|閱讀 1017 次
概述:DevExpress Winforms Controls 內(nèi)置140多個UI控件和庫,完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,本文將為大家介紹如何在DevExpress程序中實(shí)現(xiàn)數(shù)據(jù)展示。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress Winforms Controls 內(nèi)置140多個UI控件和庫,完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序。DevExpress WinForm v20.2全新發(fā)布,想要體驗(yàn)?點(diǎn)擊下載>>
在一些數(shù)據(jù)的即時查詢場景中,我們可能需要對輸入信息進(jìn)行模糊查詢并進(jìn)行選擇,例如在一些文本輸入場景,如輸入某個站點(diǎn)編碼或者設(shè)備編碼,然后獲取符合的列表供用戶選擇的場景,本篇隨筆介紹在DevExpress程序中使用PopupContainerEdit和PopupContainer實(shí)現(xiàn)數(shù)據(jù)展示。
在DevExpress中,我們?nèi)绻枰玫捏w驗(yàn)效果也可以用SearchLookupEdit來實(shí)現(xiàn)數(shù)據(jù)的查詢及展示,不過這個控件,需要提前準(zhǔn)備好數(shù)據(jù)源,然后是基于固定的數(shù)據(jù)源進(jìn)行搜索的,如下所示。
 
 
這種可以在編輯框里面輸入數(shù)據(jù),并且可以實(shí)時根據(jù)輸入的內(nèi)容進(jìn)行過濾,是一種比較好的搜索體驗(yàn),不過不好的地方就是數(shù)據(jù)需要提前預(yù)先加載,如果數(shù)據(jù)庫有成千上萬條記錄,那么這種方式弊端就比較明顯了,因此不是很適合大數(shù)據(jù),而且能夠即時進(jìn)行數(shù)據(jù)搜索展示的場景。
除了第一點(diǎn)的搜索方式外,也可以使用一種文本和按鈕合并的控件來實(shí)現(xiàn)數(shù)據(jù)的查詢選擇,控件名稱為ButtonEdit,界面效果如下所示。
 
 
當(dāng)我們單擊文本輸入的右側(cè)按鈕控件后,可以讓它彈出一個對話框進(jìn)行數(shù)據(jù)的選擇,對話框窗體里面可以根據(jù)條件進(jìn)行數(shù)據(jù)的分頁查詢,這種方式可以很好實(shí)現(xiàn)多條件的查詢選擇,雙擊記錄選擇好就關(guān)閉窗體界面即可。
 
 
上面的按鈕在設(shè)計界面里面,為相關(guān)的事件添加代碼即可。
 
 
實(shí)現(xiàn)上面功能界面的代碼很簡單,如下所示。
private void txtOfferNum_Properties_Click(object sender, EventArgs e)
{
FrmSelectOffer dlg = new FrmSelectOffer();
if(dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var info = dlg.OfferInfo;
if(info != null)
{
this.txtOfferNum.Text = info.OfferNum;
除了上面界面的選擇方式外,在DevExpress里面,我們也可以使用 PopupContainerEdit和PopupContainer實(shí)現(xiàn)數(shù)據(jù)展示,這種方式好處就是可以在錄入的時候進(jìn)行及時查詢,而且數(shù)據(jù)是即時加載的,不會一次性加載所有的數(shù)據(jù),為了演示這種方式的界面處理,我做了一個小案例,如下所示。
 
 
這種方式的展示,會及時列出相關(guān)的數(shù)據(jù),在表格控件上選擇后返回主界面。
 
 
如果按鍵Esc,那么關(guān)閉彈出層并切換到輸入層,重新輸入,回車后進(jìn)行查詢。
 
 
首先在代碼處理中,需要對輸入控件的按鍵進(jìn)行處理。
/// <summary>
/// 對按鍵進(jìn)行相關(guān)處理
/// </summary>
private void popupContainerEdit1_KeyPress(object sender, KeyPressEventArgs e)
{
this.popupContainerEdit1.ShowPopup();
//回車的時候綁定數(shù)據(jù)源,并設(shè)置
if (e.KeyChar == '\r')
{
BindData();
this.gridView1.Focus();
canAcceptReturn = false;
}
else
{
this.ActiveControl = this.popupContainerEdit1;
this.popupContainerEdit1.Focus();
}
}
在輸入回車的時候,我們執(zhí)行數(shù)據(jù)查詢操作。
我們這里測試了對數(shù)據(jù)字典的查詢顯示,只是為了演示數(shù)據(jù)的即時查詢操作。
/// <summary>
/// 綁定GridView的數(shù)據(jù)源
/// </summary>
private void BindData()
{
var value = this.popupContainerEdit1.Text;
this.lblName.Text = value;
string condition = string.Format("Name like '%{0}%'", value);
var list = BLLFactory<DictData>.Instance.Find(condition);
this.gridView1.Columns.Clear();
this.gridView1.CreateColumn("Name", "名稱", 200, false);
this.gridView1.CreateColumn("Value", "字典值", 200, false);
this.gridView1.CreateColumn("Seq", "排序", 80, false);
this.gridControl1.DataSource = list;
}
為了實(shí)現(xiàn)在列表中單擊或者使用回車鍵進(jìn)行選擇,我們對相關(guān)的事件進(jìn)行了處理。
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
GetSelectValue();
}
private void gridControl1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (canAcceptReturn)
{
GetSelectValue();
}
canAcceptReturn = true;
}
}
private void GetSelectValue(bool closePopup = true)
{
var value = string.Concat(this.gridView1.GetFocusedRowCellValue("Name"));
if (closePopup)
{
this.popupContainerEdit1.ClosePopup();
}
this.popupContainerEdit1.Text = value;
}
一旦容器焦點(diǎn)消失,我們讓焦點(diǎn)重新回到輸入控件上,如下代碼實(shí)現(xiàn)。
private void popupContainerControl1_Leave(object sender, EventArgs e)
{
//容器退出的時候,重新定位焦點(diǎn)到編輯框
this.popupContainerEdit1.Focus();
}
整個案例代碼如下所示。
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
/// <summary>
/// 設(shè)置一個標(biāo)識,是否在GridView中可以接受回車鍵
/// </summary>
bool canAcceptReturn = false;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 對按鍵進(jìn)行相關(guān)處理
/// </summary>
private void popupContainerEdit1_KeyPress(object sender, KeyPressEventArgs e)
{
this.popupContainerEdit1.ShowPopup();
//回車的時候綁定數(shù)據(jù)源,并設(shè)置
if (e.KeyChar == '\r')
{
BindData();
this.gridView1.Focus();
canAcceptReturn = false;
}
else
{
this.ActiveControl = this.popupContainerEdit1;
this.popupContainerEdit1.Focus();
}
}
/// <summary>
/// 綁定GridView的數(shù)據(jù)源
/// </summary>
private void BindData()
{
var value = this.popupContainerEdit1.Text;
this.lblName.Text = value;
string condition = string.Format("Name like '%{0}%'", value);
var list = BLLFactory<DictData>.Instance.Find(condition);
this.gridView1.Columns.Clear();
this.gridView1.CreateColumn("Name", "名稱", 200, false);
this.gridView1.CreateColumn("Value", "字典值", 200, false);
this.gridView1.CreateColumn("Seq", "排序", 80, false);
this.gridControl1.DataSource = list;
}
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
GetSelectValue();
}
private void gridControl1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (canAcceptReturn)
{
GetSelectValue();
}
canAcceptReturn = true;
}
}
private void GetSelectValue(bool closePopup = true)
{
var value = string.Concat(this.gridView1.GetFocusedRowCellValue("Name"));
if (closePopup)
{
this.popupContainerEdit1.ClosePopup();
}
this.popupContainerEdit1.Text = value;
}
private void popupContainerControl1_Leave(object sender, EventArgs e)
{
//容器退出的時候,重新定位焦點(diǎn)到編輯框
this.popupContainerEdit1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
本文轉(zhuǎn)載自
DevExpress技術(shù)交流群2:775869749 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: