Devexpress,RepositoryItemComboBox有多个列,如RepositoryItemGridLookUpEdit

时间:2016-03-03 08:06:51

标签: c# devexpress

我希望在单击列中的单元格后显示包含多个列的网格(如RepositoryItemGridLookUpEdit),但如果用户不想从网格中选择项目,则可以编写其他内容(如RepositoryItemComboBox)。如何结合这两个功能?

//user can write, but only one column
RepositoryItemComboBox cbeMaterialy = new RepositoryItemComboBox();
DataTable dt = Getdt();
cbeMaterialy.Items.Clear();

foreach(DataRow item in dt.Rows)
{
    cbeMaterialy.Items.Add(item);
}

gvView.Columns["ColumnName"].ColumnEdit = cbeMaterialy;



//user cannot write but multiple columns
RepositoryItemGridLookUpEdit editor = new RepositoryItemGridLookUpEdit();
editor.DataSource = dt;
column.ColumnEdit = editor;

// SOLUTION! 我混合了Devexpress团队的一些答案并提出了这个问题:

public class Main()
    {
        //user can choose element from DB or write new value
        RepositoryItemGridLookUpEdit riglue = new RepositoryItemGridLookUpEdit();
        MyGridLookupDataSourceHelper.SetupGridLookUpEdit(riglue, GetMaterialyDataView(), "Kod", "Kod");
        elementsEditGrid.gvView.Columns[ColumnName].ColumnEdit = riglue;
    }


///////////////////

public class MyGridLookupDataSourceHelper
{
    RepositoryItemGridLookUpEdit edit;

    public MyGridLookupDataSourceHelper(RepositoryItemGridLookUpEdit edit, ITypedList dataSource, string displayMember, string valueMember)
    {
        this.edit = edit;

        //enable writing into RepositoryItemGridLookUpEdit
        edit.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;

        edit.DataSource = dataSource;
        edit.DisplayMember = displayMember;
        edit.ValueMember = valueMember;
        edit.ProcessNewValue += edit_ProcessNewValue;

        AddExistingValuesToDataSource();
    }

    public static void SetupGridLookUpEdit(RepositoryItemGridLookUpEdit edit, ITypedList dataSource, string displayMember, string valueMember) 
    {
        new MyGridLookupDataSourceHelper(edit, dataSource, displayMember, valueMember);
    }

    //Add new values to temporary data source (not to DataBase!)
    void edit_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
    {
        GridLookUpEdit lookUp = sender as GridLookUpEdit;
        RepositoryItemGridLookUpEdit ri = lookUp.Properties;
        DataTable dt = (ri.DataSource as DataView).Table;
        DataRow row = dt.NewRow();
        row[ri.DisplayMember] = e.DisplayValue;
        row[ri.ValueMember] = e.DisplayValue;
        dt.Rows.Add(row);
        ri.View.RefreshData();
        e.Handled = true;

        //if user wants to add new values to database (data source)
        //INSERT INTO DataSourceTable (ColumnName) VALUES (e.DisplayValue)
    }

    //Adds to temporary data source values already stored in RepositoryItemGridLookUpEdit (in case values aren't from DB)
    void ProcessExistingValues(RepositoryItemGridLookUpEdit sender, object value)
    {
        RepositoryItemGridLookUpEdit ri = sender;
        DataTable dt = (ri.DataSource as DataView).Table;
        DataRow row = dt.NewRow();
        row[ri.DisplayMember] = value;
        row[ri.ValueMember] = value;
        dt.Rows.Add(row);
        ri.View.RefreshData();
    }

    private void AddExistingValuesToDataSource()
    {
        //SELECT from DB values that are already in riglue and add them to TEMPORARY DATA SOURCE (in case values aren't from DB)
        //DataView dataView = SELECT ColumnName FROM table WHERE rowID = XXX

        foreach (DataRow row in dataView.AsEnumerable())
        {
            string kod = (string)row["ColumnName"];

            ProcessExistingValues(this.edit, kod);
        }
    }
}

public class Main() { //user can choose element from DB or write new value RepositoryItemGridLookUpEdit riglue = new RepositoryItemGridLookUpEdit(); MyGridLookupDataSourceHelper.SetupGridLookUpEdit(riglue, GetMaterialyDataView(), "Kod", "Kod"); elementsEditGrid.gvView.Columns[ColumnName].ColumnEdit = riglue; } /////////////////// public class MyGridLookupDataSourceHelper { RepositoryItemGridLookUpEdit edit; public MyGridLookupDataSourceHelper(RepositoryItemGridLookUpEdit edit, ITypedList dataSource, string displayMember, string valueMember) { this.edit = edit; //enable writing into RepositoryItemGridLookUpEdit edit.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard; edit.DataSource = dataSource; edit.DisplayMember = displayMember; edit.ValueMember = valueMember; edit.ProcessNewValue += edit_ProcessNewValue; AddExistingValuesToDataSource(); } public static void SetupGridLookUpEdit(RepositoryItemGridLookUpEdit edit, ITypedList dataSource, string displayMember, string valueMember) { new MyGridLookupDataSourceHelper(edit, dataSource, displayMember, valueMember); } //Add new values to temporary data source (not to DataBase!) void edit_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e) { GridLookUpEdit lookUp = sender as GridLookUpEdit; RepositoryItemGridLookUpEdit ri = lookUp.Properties; DataTable dt = (ri.DataSource as DataView).Table; DataRow row = dt.NewRow(); row[ri.DisplayMember] = e.DisplayValue; row[ri.ValueMember] = e.DisplayValue; dt.Rows.Add(row); ri.View.RefreshData(); e.Handled = true; //if user wants to add new values to database (data source) //INSERT INTO DataSourceTable (ColumnName) VALUES (e.DisplayValue) } //Adds to temporary data source values already stored in RepositoryItemGridLookUpEdit (in case values aren't from DB) void ProcessExistingValues(RepositoryItemGridLookUpEdit sender, object value) { RepositoryItemGridLookUpEdit ri = sender; DataTable dt = (ri.DataSource as DataView).Table; DataRow row = dt.NewRow(); row[ri.DisplayMember] = value; row[ri.ValueMember] = value; dt.Rows.Add(row); ri.View.RefreshData(); } private void AddExistingValuesToDataSource() { //SELECT from DB values that are already in riglue and add them to TEMPORARY DATA SOURCE (in case values aren't from DB) //DataView dataView = SELECT ColumnName FROM table WHERE rowID = XXX foreach (DataRow row in dataView.AsEnumerable()) { string kod = (string)row["ColumnName"]; ProcessExistingValues(this.edit, kod); } } }

2 个答案:

答案 0 :(得分:0)

您可以将RepositoryItemGridLookUpEdit.TextEditStyle属性设置为标准,然后您就可以输入。当用户键入与查找数据源中包含的内容不同的内容时,将引发RepositoryItemGridLookUpEdit.ProcessNewValue事件。在事件处理程序中,您可以向数据源添加新值以进一步使用它。

答案 1 :(得分:0)

RepositoryItemComboBox不具有DataSourse属性,该属性允许将表与多列绑定。

ComboBoxEdit具有DataSourse属性,因此您可以将表与多列绑定,或创建多列。