C# DevExpress教学 GridLookUpEdit组件表格下拉组件使用
目录
开发环境
- VS2022+DevExpress v22.2.3+ .NET6 WinFormApps
- GridLookUpEditDemo.sln
Demo效果
自动搜索过滤
支持输入新增,自动添加到数据源
适用场景
- 快速搜索,输入关键数据自动弹出下拉窗体,并自动定位记录。
- 输入新值,若下拉窗体没有用户想要的数据,支持输入新值自动添加到下拉表格的数据源。
C#源码
初始化GridLookUpEdit组件
C# 全选
/// <summary>
/// 初始化GridLookUpEdit组件
/// </summary>
/// <param name="rep"></param>
private void InitGridLookUpEdit(RepositoryItemGridLookUpEdit rep)
{
GridView view = rep.View;
view.Columns.Add(new GridColumn { Caption = "货号", FieldName = "GoodsNo", Width = 100, VisibleIndex = 0 });
view.Columns.Add(new GridColumn { Caption = "品名", FieldName = "ProductName", Width = 200, VisibleIndex = 1 });
view.Columns.Add(new GridColumn { Caption = "客户", FieldName = "CustomerName", Width = 100, VisibleIndex = 2 });
rep_Grid.PopupFormSize = new Size(450, 300);//下拉窗体尺寸
rep_Grid.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.True; //重要!!!接受文本框的值作为新值显示
rep_Grid.View.RowHeight = 22; //行高
rep_Grid.ImmediatePopup = true;//输入值立即弹出下拉窗体
rep_Grid.SearchMode = GridLookUpSearchMode.AutoSearch;//设置为自动搜索模式,重要!!!
rep_Grid.PopupFilterMode = DevExpress.XtraEditors.PopupFilterMode.Contains;//表格筛选列过滤模式
rep_Grid.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;//允许录入资料
rep_Grid.View.OptionsView.ShowAutoFilterRow = true;//下拉表格显示过滤行
//绑定下拉窗体数据源
rep_Grid.DataSource = DemoData.GetGoodsList();
rep_Grid.ProcessNewValue += OnGrid_ProcessNewValue; //在输入框录入新值处理事件
}
RepositoryItemGridLookUpEdit.ProcessNewValue事件
C# 全选
/// <summary>
/// 输入新值,若下拉窗体没有匹配数据,自动添加到下拉表格的数据源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnGrid_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
{
var editor = sender as GridLookUpEdit;
//处理空值
if (e.DisplayValue == null || String.IsNullOrWhiteSpace(e.DisplayValue.ToString()) || e.DisplayValue.ToString() == editor.Properties.NullText)
{
return;
}
//数据源添加一条记录
var list = editor.Properties.DataSource as List<GoodsItem>;
list.Add(new GoodsItem { GoodsNo = e.DisplayValue.ToString() });//这里必须是对应DisplayMember字段
e.Handled = true;
}
Form初始化
C# 全选
public Form1()
{
InitializeComponent();
InitGridLookUpEdit(rep_Grid);
}
private void Form1_Load(object sender, EventArgs e)
{
//设置显示字段与取值字段名称
//DisplayMember:显示数据的字段,显示数据源中该字段的数据。
//ValueMember:取值字段,用户在下拉窗体中选择一条记录,获取该字段的值并赋值给数据源。
rep_Grid.DisplayMember = "GoodsNo";
rep_Grid.ValueMember = "GoodsNo";
gridControl1.DataSource = DemoData.GetGridData();
}
模型
C# 全选
internal class DemoData
{
/// <summary>
/// 表格数据源
/// </summary>
/// <returns></returns>
public static List<GoodsItem> GetGridData()
{
var result = new List<GoodsItem>()
{
new GoodsItem{ GoodsNo="G001", ProductName="鼠标01", Qty=200 },
new GoodsItem{ GoodsNo="G=A01", ProductName="键盘102", Qty=105 },
new GoodsItem{ GoodsNo="Xa-99", ProductName="机箱GameBox", Qty=100 },
};
return result;
}
/// <summary>
/// 表格下拉窗体数据源
/// </summary>
/// <returns></returns>
public static List<GoodsItem> GetGoodsList()
{
var result = new List<GoodsItem>()
{
new GoodsItem{ CustomerName="联想", GoodsNo="G001", ProductName="鼠标01", Qty=200 },
new GoodsItem{ CustomerName="ASUS",GoodsNo="A=AC01", ProductName="键盘102", Qty=105 },
new GoodsItem{ CustomerName="DELL",GoodsNo="D9B9", ProductName="键盘102", Qty=100 },
new GoodsItem{ CustomerName="ACER",GoodsNo="AXa001", ProductName="机箱GameBox", Qty=100 },
new GoodsItem{ CustomerName="ACER",GoodsNo="AX8B70", ProductName="键盘A102", Qty=100 },
new GoodsItem{ CustomerName="联想",GoodsNo="GXzC", ProductName="鼠标A01", Qty=100 },
new GoodsItem{ CustomerName="ASUS",GoodsNo="AXa99B", ProductName="机箱GameBox", Qty=100 },
new GoodsItem{ CustomerName="联想",GoodsNo="GXa2", ProductName="鼠标B01", Qty=100 },
};
return result;
}
}
public class GoodsItem
{
public string CustomerName { get; set; }
public string ProductName { get; set; }
public string GoodsNo { get; set; }
public int Qty { get; set; }
}
ERP系统-销售订单应用
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网