在GridControl中使用GridLookUpEdit的正确姿势

先上效果图

 

效果图

操作过程:

点击红框那一列的任何一格,都会弹出如绿框所示的数据选择列表(这个列表长的和GridView一样有木有,是的,他就是GridView),在弹出框中选择数据后,会将想要显示的数据显示在红框处。

这两者组合可以实现,在GridControl的数据列表中进行编辑和选择数据。

下面开始说明使用流程。

1. 点击进入GridControl的Run Designer视图

1

2. 找到如下模块,点击进入

 

3. 新建一个GridLookUpEdit控件(这个新建控件和用鼠标拖动一个控件到界面上新建控件的本质是一样的,都可以在代码里通过[name]在引用到他。)

从图中可以看出,可以新建任意控件,在这里新建的控件等下会用到。

 

如下图,glueCategory3是我创建来准备做选择三级分类的,还有请注意图中右边的三个红框(非常重要的三个属性)

(name):glueCategory3: 在代码中用这个名字引用该控件

DisplayMember:这个属性值决定了在弹出的控件里选择某行之后,在GridControl显示的值是什么。

ValueMember:这个属性值决定了在在弹出的控件里选择某行之后,控件的Editvalue,并且这个值还会传进事件里,同时,这个值还要和GridControl中的某列的FieldName值对应,否则会无法显示值

总之,这几个值很重要,如果在设置过程中出现数据选择了之后没有反应,八成是这几个值设置的有问题。

关于这几个值,后面还有用到,我们把这几个值命名为《重要三值》

 

效果图

4.在In-place EditorRepository视图中新建好一个GridControl控件后,切换到列视图。

 

5.我选择的是在《三级分类》列里增加GridLookUpEdit控件,选择了《三级分类》列后,在左边的属性视图中,找到ColumnEdit属性,点击最左边的下拉按钮,选择刚刚创建的glueCategory3控件。

 

效果图

6.设置《三级分类》的FieldName值,如上图,在ColumnEdit的下面就是FieldName属性值,这个属性值设置要谨慎,他的值务必和第三步所说的重要三值中的ValueMember相对应,否则会出现无法显示值的空白。

PS:我就是踩到这个错误,我把该FieldName设置成了别的值,然后再GridControl中死活无法显示出数据。

7. 设置GridLookUpEdit的数据源,这个在代码里直接赋值即可。

 

代码

8.还有最后一步,设置GridLookUpEdit的GriView(这是属于GridLookUpEdit的范畴了)只有设置了这个,下拉宽才会显示你想要的值,否则可能会显示出你赋值给他的数据源的所有值

 

a

 

9.最后的最后是事件参数的问题。

在GridControl的全部事件有一个Cell值发生改变时就会触发的事件,如下图所示,如果某cell已经被设置了ColumnEdit属性,那么传递到事件处理函数内部的值就是相应控件的值了,比如:如果你为某列设置了GridLookUpEdit控件,那么在你下拉选择改变了该cell时,传递到事件里的e.value值是你给GridLookUpEdit控件设置的ValueMember值。

事件视图

 

事件处理函数

 

总结

我感觉,如果给GridControl中的显示视图的某个Cell设置了ColumnEdit属性之后,该Cell就会完全变成了该控件,所以,该控件拥有的功能它都有,并且还加上了原本GridControl的事件功能。



DevExpress.XtraEditors.GridLookUpEdit使用详情2  https://www.jianshu.com/p/7e3a26f90c32

 

LOOKupEdit GridControl

using DevExpress.XtraEditors.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            InitData();
 
            gridControl1.DataSource = Products;
            gridView1.Columns["UnitPrice"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gridView1.Columns["UnitPrice"].DisplayFormat.FormatString = "c2";
 
            // Create an in-place LookupEdit control.
            RepositoryItemLookUpEdit riLookup = new RepositoryItemLookUpEdit();
            riLookup.DataSource = Categories;
            riLookup.ValueMember = "ID";
            riLookup.DisplayMember = "CategoryName";
 
            // Enable the "best-fit" functionality mode in which columns have proportional widths and the popup window is resized to fit all the columns.
            //最佳弹出  模式
            riLookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
            // Specify the dropdown height.  设置高度
            riLookup.DropDownRows = Categories.Count;
 
            // Enable the automatic completion feature. In this mode, when the dropdown is closed,
            // the text in the edit box is automatically completed if it matches a DisplayMember field value of one of dropdown rows.
            // 自动搜索
            riLookup.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoFilter;
            // Specify the column against which an incremental search is performed in SearchMode.AutoComplete and SearchMode.OnlyInPopup modes
            riLookup.AutoSearchColumnIndex = 1;
 
            // Optionally hide the Description column in the dropdown.
            // riLookup.PopulateColumns();
            // riLookup.Columns["Description"].Visible = false;
 
            // Assign the in-place LookupEdit control to the grid's CategoryID column.
            // Note that the data types of the "ID" and "CategoryID" fields match.
            //绑定到某列中
            gridView1.Columns["CategoryID"].ColumnEdit = riLookup;
            gridView1.BestFitColumns();
        }
 
        List<Product> Products = new List<Product>();
        List<Category> Categories = new List<Category>();
 
        private void InitData()
        {
            Products.Add(new Product() { ProductName = "Sir Rodney's Scones", CategoryID = 3, UnitPrice = 10 });
            Products.Add(new Product() { ProductName = "Gustaf's Knäckebröd", CategoryID = 5, UnitPrice = 21 });
            Products.Add(new Product() { ProductName = "Tunnbröd", CategoryID = 5, UnitPrice = 9 });
            Products.Add(new Product() { ProductName = "Guaraná Fantástica", CategoryID = 1, UnitPrice = 4.5m });
            Products.Add(new Product() { ProductName = "NuNuCa Nuß-Nougat-Creme", CategoryID = 3, UnitPrice = 14 });
            Products.Add(new Product() { ProductName = "Gumbär Gummibärchen", CategoryID = 3, UnitPrice = 31.23m });
            Products.Add(new Product() { ProductName = "Rössle Sauerkraut", CategoryID = 7, UnitPrice = 45.6m });
            Products.Add(new Product() { ProductName = "Thüringer Rostbratwurst", CategoryID = 6, UnitPrice = 123.79m });
            Products.Add(new Product() { ProductName = "Nord-Ost Matjeshering", CategoryID = 8, UnitPrice = 25.89m });
            Products.Add(new Product() { ProductName = "Gorgonzola Telino", CategoryID = 4, UnitPrice = 12.5m });
 
            Categories.Add(new Category() { ID = 1, CategoryName = "Beverages", Description = "Soft drinks, coffees, teas, beers, and ales" });
            Categories.Add(new Category() { ID = 2, CategoryName = "Condiments", Description = "Sweet and savory sauces, relishes, spreads, and seasonings" });
            Categories.Add(new Category() { ID = 3, CategoryName = "Confections", Description = "Desserts, candies, and sweet breads" });
            Categories.Add(new Category() { ID = 4, CategoryName = "Dairy Products", Description = "Cheeses" });
            Categories.Add(new Category() { ID = 5, CategoryName = "Grains/Cereals", Description = "Breads, crackers, pasta, and cereal" });
            Categories.Add(new Category() { ID = 6, CategoryName = "Meat/Poultry", Description = "Prepared meats" });
            Categories.Add(new Category() { ID = 7, CategoryName = "Produce", Description = "Dried fruit and bean curd" });
            Categories.Add(new Category() { ID = 8, CategoryName = "Seafood", Description = "Seaweed and fish" });
        }
    }
 
    public class Product
    {
        public string ProductName { get; set; }
        public decimal UnitPrice { get; set; }
        public int CategoryID { get; set; }
    }
 
    public class Category
    {
        public int ID { get; set; }
        public string CategoryName { get; set; }
        public string Description { get; set; }
    }
 
}
   绑定的列和lookupedit 不要同名  *

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值