0

我需要一些有关Windows FormsDataGridView课程的帮助,我不知道是不是只有我一个人,但我真的以编程方式在这门课上苦苦挣扎。

您会认为将 aColumn作为 a添加ComboBox到a 的简单任务DataGridView将是小菜一碟,但显然事实并非如此!我需要以编程方式执行此操作的原因是因为我需要在 DataGridView 中的每一行上都有多个 ComboBoxes,其中可选择的下拉项目将取决于其他一个或多个中的选择等......

这是我如何初始化我的DataGridView.

private void InitializeDataGridView()
{
    _objectDataGridView.AutoGenerateColumns = false;
    _objectDataGridView.Columns.Add(new DataGridViewTextBoxColumn
                                        {
                                                DataPropertyName = "Id",
                                                HeaderText = "Id",
                                                ValueType = typeof(int)
                                        });
    _objectDataGridView.Columns.Add(new DataGridViewTextBoxColumn
                                        {
                                                DataPropertyName = "Name",
                                                HeaderText = "Name",
                                                ValueType = typeof(string),
                                                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                                        });
    var objectTypeComboBoxColumn = new DataGridViewComboBoxColumn
                                       {
                                               DataPropertyName = "Type",
                                               HeaderText = "Object Type",
                                               ValueType = typeof(ObjectType),
                                               ValueMember = "Id",
                                               DisplayMember = "Name",
                                               DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
                                       };
    _objectDataGridView.Columns.Add(objectTypeComboBoxColumn);
    var containerComboBoxColumn = new DataGridViewComboBoxColumn
                                      {
                                              DataPropertyName = "Container",
                                              HeaderText = "Container",
                                              ValueType = typeof(Container),
                                              ValueMember = "Id",
                                              DisplayMember = "Name",
                                              DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
                                      };
    _objectDataGridView.Columns.Add(containerComboBoxColumn);
}

到目前为止,一切都很好。现在,当我在我正在听的事件中单击一个TreeNodeDecorator(派生自TreeNode)时。然后,我使用Fluent NHibernate ( ORM )检索关联对象的列表。TreeViewOnBeforeSelectTreeNodeDecorator.Id

使用新检索到的列表,我想清除然后_objectDataGridView用 new填充Rows。在监听OnBeforeSelect事件的方法中,我尝试设置_objectDataGridView. DataSource像这样。

private void SetDataSource(IEnumerable<Object> assignedObjects)
{
    var dataTable = new DataTable();
    dataTable.Columns.Add("Id");
    dataTable.Columns.Add("Name");
    dataTable.Columns.Add("Type");
    dataTable.Columns.Add("Container");
    foreach(var assignedObject in assignedObjects)
    {
        dataTable.Rows.Add(assignedObject.Id,
                           assignedObject.Name,
                           assignedObject.ObjectType,
                           assignedObject.Container);
    }
    _objectDataGridView.DataSource = dataTable;
}

但我还没有为DataGridViewComboBoxColumns. 在创建 时,我可以设置一组静态的可选项目DataGridViewComboBoxColumns,但我需要根据同一行上其他组合框中的选定项目,每行动态地单独添加这些项目。我真的不知道该怎么做。

我想我需要听一个事件,然后从那里用正确的项目填充DataGridViewComboBoxColumns.Itemsor 。DataGridViewComboBoxColumns.DataSource然后我还需要听这些中的选择何时更改ComboBoxes并填写/更改相关的可选项目ComboBoxes

4

1 回答 1

0

我最终添加RowsDataGridView这样的:

private void SetDataSource(IEnumerable<Object> assignedObjects, 
                           List<Container> subContainersAssociatedWithSystem)
{
    _objectDataGridView.Rows.Clear();
    foreach (var assignedObject in assignedObjects)
    {
        var newRowIndex = _objectDataGridView.Rows.Add();
        var row = _objectDataGridView.Rows[newRowIndex];
        row.Cells["Id"].Value = assignedObject.Id;
        row.Cells["Name"].Value = assignedObject.Name;
        var containerColumn = (DataGridViewComboBoxCell)row.Cells["Container"];
        containerColumn.DataSource = subContainersAssociatedWithSystem;
        containerColumn.Value = assignedObject.Container.Id;
        var objectTypeColumn = (DataGridViewComboBoxCell)row.Cells["Type"];
        objectTypeColumn.DataSource = new List<ObjectType> {assignedObject.ObjectType};
        objectTypeColumn.Value = assignedObject.ObjectType.Id;
    }
}

然后像这样收听EditingControlShowing事件:

private void InitializeDataGridView()
{
    ...
    _objectDataGridView.EditingControlShowing += (sender, e) =>
    {
        var cb = e.Control as ComboBox;
        if (_objectDataGridView.CurrentCellAddress.X == objectTypeComboBoxColumn.DisplayIndex && cb != null)
        {
            var value = _objectDataGridView[containerComboBoxColumn.DisplayIndex, _objectDataGridView.CurrentCellAddress.Y].Value;
            if(value != null)
            {
                var containerId = (int)value;
                using(var dao = _daoFactory.Create(_daoAdminRole))
                {
                    var container = dao.Get<Container>(containerId);
                    var objectTypes = dao.GetByQueryObject(new ObjectTypeQueryObject {ContainerType = new ContainerType {Id = container.ContainerType.Id}});
                    cb.DataSource = objectTypes;
                }
            }
        }
    };
    ...
}

现在,当objectTypeComboBoxColumnDataSource根据containerComboBoxColumn.

于 2013-08-14T10:50:02.183 回答