DevExpress:XtraGridView自定义列绑定

时间:2011-09-11 13:28:45

标签: c# devexpress xtragrid databound

让我们说我有以下两个类

public class OtherClass
{
    public string OtherName{ get; set; }
}

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

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

现在,我的方案是,当我点击来自TheName对象列表的Editor button OtherName的{​​{1}}时,我想填充第一列(OtherClassList<OtherClass>

然后最终将整行转回MyClass对象

可能吗?

1 个答案:

答案 0 :(得分:0)

也许是这样的:

        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" });

通过设计师

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

你的数据应该没问题