如何在DataGridViewComboBoxCell中选择一个值? [英] How do I select a value in a DataGridViewComboBoxCell?

查看:109
本文介绍了如何在DataGridViewComboBoxCell中选择一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DataGridViewComboBoxCell和一个DataTable。表I中的数据使用DataSource与DataGridViewComboBoxCell绑定,并设置ValueMember和DisplayMember。

I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell using DataSource and set ValueMember, and DisplayMember.

private void Form1_Load(object sender, EventArgs e)
{         
    DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();

    dataGridView1.Rows[0].Cells[0] = comboBoxCell;

    comboBoxCell.DataSource = dataTable;
    comboBoxCell.ValueMember = "ID";
    comboBoxCell.DisplayMember = "Item";
}

如何在加载表格时以编程方式设置单元格中的值?
在简单的ComboBox中,我知道一个属性SelectedIndex。
我尝试了comboBoxCell.Value = ...;但这是一个例外。
并尝试

How can I programmatically set the value in the cell when the form loads? In the simple ComboBox I know a property SelectedIndex. I tried comboBoxCell.Value = ...; but it gives an exception. And tried

private void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    e.Value = 1;
}

它在单元格中设置了一个新值,但是我需要选择一个值

It sets a new value in the cell, but I need to select a value.

表格已加载,我的单元格为空。

Form loaded and I have empty cell.

在组合框中有一些数据。

And some data in the ComboBox.

当我将这段代码放在 dataGridView1.Rows [0] .Cells [ ComboColumn]。Value = 1; 之后。 DisplayMember = ...(请参见上文)。

When I put this code dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1"; right after comboBoxCell.DisplayMember = ... (see above), it works fine.

ID列中的值 1对应于Items列中的值 Second。因此,我得到了正确的结果。

The value "1" in the ID column corresponds to the value "Second" in the Items column.So, I get the correct result.

抱歉,我的英语和我的新手代码:)

Sorry for my English and my newbie code :)

推荐答案

不是将单元格添加到网格中,而是添加 DataGridViewComboBox 列。

Instead of adding a cell to your grid add a DataGridViewComboBox column.

DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Name = "ComboColumn";
c.DataSource = dataTable;
c.ValueMember = "ID";
c.DisplayMember = "Item";
dataGridView1.Columns.Add(c);

要选择特定值,请设置给定单元格的Value属性。

To select a particular value you set the Value property of a given cell.

dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;




  • 请注意,此处的类型很重要!在注释中,您说得到一个 System.FormatException

    当您将值设置为1时,您将分配一个int值-如果由于某些原因您在输入值中包含字符串在ID列中,您会看到 System.FormatException 异常。

    When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.

    如果类型不同,则需要更新DataTable定义或将值设置为字符串:

    If the types differ you need to either update the DataTable definition or set the value to a string:

    dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";
    


  • 还请注意,此值必须存在设置为网格源的DataTable。

  • Also note that this value must be present in the ID column of the DataTable that you have set as the source of the grid.
  • 通常使用 DataGridView 设置了数据源。在这种情况下,您可以使用DataPropertyName属性将ComboBoxColumn绑定到网格的DataSource。

    It is usually easiest to work with a DataGridView when it has its DataSource set. In this case you can bind the ComboBoxColumn to the grid's DataSource using the DataPropertyName property.

    c.DataPropertyName = "GridDataSourceColumnName";
    

    这允许从网格数据源获取列值,并且可以直接更改列

    This allows the columns value to be taken from the grid data source and for changes to the column to directly change that data source.

    最后,请勿在此处使用CellFormatting事件-此事件不适用于此类事件用。通常最好在DataBindingComplete事件中(如果您只希望完成一次)或在需要DefaultValues或RowValidating之类的事件期间进行此类工作。

    Lastly, do not use the CellFormatting event here - this event is not intended for this sort of use. It is usually best to do this sort of work in the DataBindingComplete event (if you only want it done once) or during some event like DefaultValues needed or RowValidating.

    使用CellFormatting可能会使用户无法手动编辑组合框。

    By using CellFormatting you will probably make it impossible for users to manually edit the combo box.

    这篇关于如何在DataGridViewComboBoxCell中选择一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆