C# Datagridview:获取组合框列中的选定项目

发布于 2024-11-07 23:34:53 字数 753 浏览 3 评论 0原文

我正在开发一个允许用户操作 xml 文件的 GUI。我在 datagridview 中显示 xml 文件,通过 xml 元素按列整齐地组织。我允许用户添加列作为我的项目的扩展。该列被添加到数据集表中,然后更新到我用来显示 xml 文件的 datagridveiew。我已经包含了用户添加组合框列来选择选项的功能,而不是像这样不断地输入它们。是真是假。然而,这正是问题所在。保存普通色谱柱很容易。组合框列很痛苦。

我有一个“保存组合框列”,可将其更新为 xml,还有一个“保存”按钮,可保存在用户选择的目的地中。

我做了一些研究,似乎组合框类具有这样的功能,可以访问用户输入的组合框中的选定项目。 我们有:

    ComboBox box = new ComboBox();
    box.SelectedItem;

我尝试将其应用于组合框列类,但它没有这样的功能。因此,我无法弄清楚如何直接获取用户所选项目的值。我也尝试过使用组合框单元进行实验,但这也没有使我有任何结果。我玩过的这两个类都没有...“所选项目”功能,甚至谷歌也没有为我提供解决方案。 =( 我也尝试过使用 cell.value,但由于某种原因它是“null”。即使用户在框中选择一个项目,它也不会保存到单元格的值中。TLDR

: 简而言之,我的问题是,如果可能的话,如何访问组合框列单元格的选定项目?此外,如何确保项目值保存在单元格中?

提前致谢。我通过 Visual Studio 2008 C# 使用 .NET 3.5 SP1。

此致,

tf.rz

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.

I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.

I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:

    ComboBox box = new ComboBox();
    box.SelectedItem;

I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.

TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?

Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.

Sincerely,

tf.rz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

束缚m 2024-11-14 23:34:53

DataGridView 中的控件不是 ComboBox,它是 DataGridViewComboBox,并且具有不同的属性和方法。来自 MSDN

与 ComboBox 控件不同,DataGridViewComboBoxCell 没有 SelectedIndex 和 SelectedValue 属性。相反,从下拉列表中选择一个值可设置单元格的“值”属性。

但是,您提到 Cell.Value 对您来说为空。好吧,根据以下文章,您可能还缺少另一个步骤(如何:访问 Windows 窗体 DataGridViewComboBoxCell 下拉列表中的对象)。

您必须将 DataGridViewComboBoxColumn.ValueMember 或 DataGridViewComboBoxCell.ValueMember 属性设置为业务对象上的属性名称。当用户进行选择时,业务对象的指示属性会设置单元格 Value 属性。

The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).

You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.

爱冒险 2024-11-14 23:34:53

如果我们将 datagridcomboboxcell 与不同的 DisplayMemberValueMember 绑定,如下所示:

dgcombocell.DisplayMember = "Name"; 
dgcombocell.ValueMember = "Id";  
dgcombocell.DataSource = dataset1.Tables[0];

然后获取 SelectedText,并且SelectedValue,我们可以编写这段代码:

string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);

希望它能解决您的问题。

If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:

dgcombocell.DisplayMember = "Name"; 
dgcombocell.ValueMember = "Id";  
dgcombocell.DataSource = dataset1.Tables[0];

Then for getting SelectedText, and SelectedValue, we can write this code:

string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);

I hope it solves your problem.

抚笙 2024-11-14 23:34:53

使用它来获取或设置选定的值:

object selectedValue = currentRow.Cells["comboboxColumnName"].Value

不要忘记为 DataGridViewComboBoxColumn 设置 DisplayMemberValueMember

Use this to get or set selected value:

object selectedValue = currentRow.Cells["comboboxColumnName"].Value

Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn

反目相谮 2024-11-14 23:34:53

是这样完成的

  DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];

  MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));

This is how it is done

  DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];

  MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
岁月染过的梦 2024-11-14 23:34:53

我用这个:

private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
     return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

I use this:

private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
     return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}
若有似无的小暗淡 2024-11-14 23:34:53

.Net 组合框实际上是由文本框和下拉列表组成的复合控件。使用box.Text获取当前显示的信息。

编辑:行或单元格应该有一个 .FindControl() 方法。您需要执行以下操作:

Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;

基本上,您在其容器(行或单元格)中查找控件,然后将找到的控件转换为组合框,然后访问其 .Text 属性。

A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.

EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:

Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;

Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.

昇り龍 2024-11-14 23:34:53

我的首选解决方案是将控件转换为普通组合框,之后 selectitem、selectedvalue 等都可用。在 vb.net 中,其中 dgvSchedule 是我的 datagridview:

    Private Sub dgvschedule_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvSchedule.EditingControlShowing

    cbx = TryCast(e.Control, ComboBox) ' Key line to manage the Datagridviewcombobox as a combobox.

My preferred solution is to cast the control as an ordinary combo box, after which selecteditem, selectedvalue, etc. are all available. In vb.net, where dgvSchedule is my datagridview:

    Private Sub dgvschedule_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvSchedule.EditingControlShowing

    cbx = TryCast(e.Control, ComboBox) ' Key line to manage the Datagridviewcombobox as a combobox.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文