点击获取工具>>
在一些数据的即时查问场景中,咱们可能须要对输出信息进行含糊查问并进行抉择,例如在一些文本输出场景,如输出某个站点编码或者设施编码,而后获取合乎的列表供用户抉择的场景,本篇随笔介绍在DevExpress程序中应用PopupContainerEdit和PopupContainer实现数据展现。
一、回顾SearchLookupEdit控件应用
在DevExpress中,咱们如果须要好的体验成果也能够用SearchLookupEdit来实现数据的查问及展现,不过这个控件,须要提前准备好数据源,而后是基于固定的数据源进行搜寻的,如下所示。
这种能够在编辑框外面输出数据,并且能够实时依据输出的内容进行过滤,是一种比拟好的搜寻体验,不过不好的中央就是数据须要提前事后加载,如果数据库有成千上万条记录,那么这种形式弊病就比拟显著了,因而不是很适宜大数据,而且可能即时进行数据搜寻展现的场景。
二、应用ButtonEdit的形式进行搜寻
除了第一点的搜寻形式外,也能够应用一种文本和按钮合并的控件来实现数据的查问抉择,控件名称为ButtonEdit,界面成果如下所示。
当咱们单击文本输出的右侧按钮控件后,能够让它弹出一个对话框进行数据的抉择,对话框窗体外面能够依据条件进行数据的分页查问,这种形式能够很好实现多条件的查问抉择,双击记录抉择好就敞开窗体界面即可。
下面的按钮在设计界面外面,为相干的事件增加代码即可。
实现下面性能界面的代码很简略,如下所示。
`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;`
三、应用PopupContainerEdit和PopupContainer
除了下面界面的抉择形式外,在DevExpress外面,咱们也能够应用 PopupContainerEdit和PopupContainer实现数据展现,这种形式益处就是能够在录入的时候进行及时查问,而且数据是即时加载的,不会一次性加载所有的数据,为了演示这种形式的界面解决,我做了一个小案例,如下所示。
这种形式的展现,会及时列出相干的数据,在表格控件上抉择后返回主界面。
如果按键Esc,那么敞开弹出层并切换到输出层,从新输出,回车后进行查问。
首先在代码解决中,须要对输出控件的按键进行解决。
`/// <summary>
/// 对按键进行相干解决
/// </summary>
private void popupContainerEdit1_KeyPress(object sender, KeyPressEventArgs e)
{
this.popupContainerEdit1.ShowPopup();
//回车的时候绑定数据源,并设置
if (e.KeyChar == 'r')
{
BindData();
this.gridView1.Focus();
canAcceptReturn = false;
}
else
{
this.ActiveControl = this.popupContainerEdit1;
this.popupContainerEdit1.Focus();
}
}`
在输出回车的时候,咱们执行数据查问操作。
咱们这里测试了对数据字典的查问显示,只是为了演示数据的即时查问操作。
`/// <summary>
/// 绑定GridView的数据源
/// </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)
{
//容器退出的时候,从新定位焦点到编辑框
this.popupContainerEdit1.Focus();
}`
整个案例代码如下所示。
`public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
/// <summary>
/// 设置一个标识,是否在GridView中能够承受回车键
/// </summary>
bool canAcceptReturn = false;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 对按键进行相干解决
/// </summary>
private void popupContainerEdit1_KeyPress(object sender, KeyPressEventArgs e)
{
this.popupContainerEdit1.ShowPopup();
//回车的时候绑定数据源,并设置
if (e.KeyChar == 'r')
{
BindData();
this.gridView1.Focus();
canAcceptReturn = false;
}
else
{
this.ActiveControl = this.popupContainerEdit1;
this.popupContainerEdit1.Focus();
}
}
/// <summary>
/// 绑定GridView的数据源
/// </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)
{
//容器退出的时候,从新定位焦点到编辑框
this.popupContainerEdit1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}`
本文转载自博客园-伍华聪[](https://home.cnblogs.com/u/wu...