C# DevExpress教学 GridLookUpEdit组件表格下拉组件使用|C/S开发框架

开发环境
- VS2022+DevExpress v22.2.3+ .NET6 WinFormApps
- GridLookUpEditDemo.sln
Demo效果

自动搜索过滤

支持输入新增,自动添加到数据源

适用场景
- 快速搜索,输入关键数据自动弹出下拉窗体,并自动定位记录。
- 输入新值,若下拉窗体没有用户想要的数据,支持输入新值自动添加到下拉表格的数据源。
C#源码
初始化GridLookUpEdit组件
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事件
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() });
e.Handled = true;
}
Form初始化
public Form1()
{
InitializeComponent();
InitGridLookUpEdit(rep_Grid);
}
private void Form1_Load(object sender, EventArgs e)
{
rep_Grid.DisplayMember = "GoodsNo";
rep_Grid.ValueMember = "GoodsNo";
gridControl1.DataSource = DemoData.GetGridData();
}
模型
internal class DemoData
{
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;
}
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系统-销售订单应用


扫一扫加作者微信