XtraGridView自定义列绑定

本文关键字:绑定 自定义 XtraGridView | 更新日期: 2023-09-27 18:08:20

假设我有以下两个类

public class OtherClass
{
    public string OtherName{ get; set; }
}
public class MyClass
{
    public string TheName { get; set; }
    public int TheAge { get; set; }
}

假设我将XtraGrid绑定到Object数据源(MyClass)

现在,我的场景是,当我点击Editor button时,我想填充第一列(TheName), OtherName值来自OtherClass对象(List<OtherClass>)列表

然后最后将整个Row转移回MyClass对象

这可能吗?

XtraGridView自定义列绑定

可能像这样:

        List<MyClass> mylist = new List<MyClass>();
        mylist.Add(new MyClass() { TheName = "Kirk", TheAge = 56 });
        mylist.Add(new MyClass() { TheName = "Spock", TheAge = 403 });
        mygrid.DataSource = mylist;
        mygrid.MainView.PopulateColumns();
        List<OtherClass> all_names = new List<OtherClass>();

添加现有名称:

        foreach (var item in mylist)
            all_names.Add( new OtherClass() { OtherName = item.TheName } );

继续添加其他名称:

        all_names.Add(new OtherClass() { OtherName = "Watson" });
        all_names.Add(new OtherClass() { OtherName = "Sherlock" });

create repositoryItemLookUpEdit1 via designer

        repositoryItemLookUpEdit1.DataSource = all_names;
        repositoryItemLookUpEdit1.DisplayMember = "OtherName";
        repositoryItemLookUpEdit1.ValueMember = "OtherName";
        ((DevExpress.XtraGrid.Views.Base.ColumnView)mygrid.MainView).Columns[0].ColumnEdit = repositoryItemLookUpEdit1;

和你的数据应该是好的