以编程方式将ComboBox添加到DataGridView

编程入门 行业动态 更新时间:2024-10-27 03:32:56
本文介绍了以编程方式将ComboBox添加到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要有关 <$ c $的帮助 Windows窗体 DataGridView 类/ a>,我不知道是否只有我一个人,但是我确实在编程上为此课程苦苦挣扎。

I need some help with the DataGridView class in Windows Forms, I don't know if it's just me but I am really struggling with this class programmatically.

您会认为,添加列作为 ComboBox 到 DataGridView 真是小菜一碟,但显然不是!我需要以编程方式执行此操作的原因是,我在DataGridView的每一行上都需要多个ComboBox,其中可选的下拉项将取决于其他一项或多项的选择,等等...

You would think that a simple task as adding a Column as a ComboBox to a DataGridView would be a piece of cake, but apparently it's not! The reason I need to do it programmatically is because I need multiple ComboBoxes on each Row in the DataGridView where the selectable drop down items will be dependent on selections in one or more of the others etc...

这是我初始化 DataGridView 。

Here is how I initialize my 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 )在 TreeView OnBeforeSelect 事件触发。然后,我使用 TreeNodeDecorator.Id 通过流利的NHibernate ( ORM )。

Up to this point everything is well and good. Now when I click a TreeNodeDecorator (derived from TreeNode) in a TreeView the OnBeforeSelect event fires of which I am listening to. I then use the TreeNodeDecorator.Id to retrieve a list of associated objects, via Fluent NHibernate (ORM).

使用新检索的列表,我想清除,然后用新的行填充 _objectDataGridView 。 在侦听 OnBeforeSelect 事件,然后尝试设置 _objectDataGridView 。 DataSource 这样。

With the newly retrieved list I want to clear and then fill the _objectDataGridView with new Rows. In the method listening to the OnBeforeSelect event I then try to set the _objectDataGridView.DataSource like this.

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 ,但是我需要根据每行中其他组合框中的选定项目动态地,单独地逐行动态添加这些项目。而且我真的不确定如何做到这一点。

But I haven't gotten to set the selectable items for the DataGridViewComboBoxColumns. I could have set a static set of selectable items when I created the DataGridViewComboBoxColumns, but I need those items to be added dynamically and individually per row and in accordance with selected items in other comboboxes on the same row. And I'm really not sure of how to do that.

我想我需要听一个事件,然后在其中填充 DataGridViewComboBoxColumns.Items 或 带有正确项目的DataGridViewComboBoxColumns.DataSource 。然后,我还需要听这些 ComboBoxes 中的选择何时更改,并填充/更改相关的 ComboBoxes 中的可选项目。 。

I guess there is an event I need to listen to, and from there fill the DataGridViewComboBoxColumns.Items or DataGridViewComboBoxColumns.DataSource with the correct items. I then also need to listen to when selections changes in these ComboBoxes and fill / change up the selectable items in related ComboBoxes.

推荐答案

我最终将行添加到 DataGridView 像这样:

I ended up adding the Rows to the DataGridView like this:

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 这样的事件:

And then listen to the EditingControlShowing event like this:

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; } } } }; ... }

现在,当我单击 objectTypeComboBoxColumn 根据 containerComboBoxColumn 。

Now when I click the objectTypeComboBoxColumn the DataSource gets set with the correct items according to the selection in the containerComboBoxColumn.

更多推荐

以编程方式将ComboBox添加到DataGridView

本文发布于:2023-07-16 20:21:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   ComboBox   DataGridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!