首页 > 解决方案 > RepositoryItemComboBox 为每一行添加特定项目

问题描述

我在 Devexpress GridView 和 4 行上有一个 RepositoryItemComboBox 控件。RepositoryItemComboBox.Items.Add 影响所有行。在 CustomRowCellEdit 和 CustomRowCellEditForEditing 事件中,我使用 RepositoryItemComboBox.Items.Clear() 和 RepositoryItemComboBox.Items.Add 但它再次影响所有行。我需要修改特定的 RepositoryItemComboBox。例如,RepositoryItemComboBox 的第一行应包含“Michael, John”,RepositoryItemComboBox 的第二行应包含“Sarah, Jake”。

标签: c#gridviewdevexpressrowgridcontrol

解决方案


您可以通过处理CustomRowCellEdit 事件来创建存储库并根据您的条件分配它。

    private RepositoryItemComboBox myRepository(string[] myNames)
    {
        RepositoryItemComboBox repositoryItemCombo = new RepositoryItemComboBox();
        repositoryItemCombo.Items.AddRange(myNames);

        return repositoryItemCombo;
    }

然后

    private void GridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
    {
        if (e.Column.FieldName != "YourFieldName")
            return;

        if (e.RowHandle == 1) // Your condition
        {
            e.RepositoryItem = myRepository(new string[] { "Michael", "John" });
        }
        else
        {
            e.RepositoryItem = myRepository(new string[] { "Sarah", "Jake" });
        }
    }

推荐阅读