获取 DatagridviewComboBoxCell 的 SelectedIndex [英] Get DatagridviewComboBoxCell's SelectedIndex

查看:40
本文介绍了获取 DatagridviewComboBoxCell 的 SelectedIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Winforms 应用程序,它有一个 DataGridView.DataGridView 未绑定到数据源.我正在读取一个文本文件,并根据文件中的每一行,将每一行的值放置到数据网格中.

I have a Winforms application which has a DataGridView. The DataGridView is not bound to a datasource. I'm reading a text file and according to each line in the file, I'm placing the values of each row to the datagrid.

我的网格中有一个列是 ComboBoxColumn.它有一个项目集合.

I have a column in my grid that is a ComboBoxColumn. It has a collection of items in it.

我的目标是将单元格中显示的项目的索引保存到文件中.但是,ComboBoxCell 似乎没有 SelectedIndex 属性,如 ComboBox.

My goal is to save to the file the index of the item that is displayed in the cell. However, it seems that ComboBoxCell doesn't have the SelectedIndex property like ComboBox.

值得一提的是,只有当用户点击保存"选项时,我才需要知道显示的项目的索引,所以我不相信editingControlShowing 事件是我要走的路.

It is important to mention that I need to know the index of the item displayed only when the user hits "Save" option, So I don't believe that editingControlShowing event is my way to go.

推荐答案

嗯,你几乎猜对了:为了找到选择的索引,你需要对 EditingControlShowing 事件进行编码,只需确保保留对编辑期间使用的 ComboBox 的引用:

Well, you got it almost right: In order to find the chosen index you do need to code the EditingControlShowing event, just make sure to keep a reference to the ComboBox that is used during the edit:

  // hook up the event somwhere:
   dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;

 // keep a reference to the editing comtrol:
 ComboBox combo = null;

 // fill the reference, once it is valid:
 void dataGridView1_EditingControlShowing(object sender, 
                                          DataGridViewEditingControlShowingEventArgs e)
 {
     combo = e.Control as ComboBox;
 }

现在你可以使用它了:

private void Save_Click(object sender, EventArgs e)
{
        int index = -1;
        if (combo != null) index = combo.SelectedIndex;
        // now do what you want..
}

注意这只是一个最小的例子.如果您的用户在按下保存"按钮之前要编辑多个列和行,您将需要存储 ComboBoxes,或者更便宜的 SelectedIndex,也许在每个单元格的 CellEndEdit 事件中.Cells 的 Tag 是明显的存储位置:

Note that this is just a minimal example. If your users will edit several columns and rows before they press the 'Save' Buton, you will need to store either the ComboBoxes, or, less expensive, the SelectedIndex, maybe in the CellEndEdit event on a per Cell basis. The Cells' Tag are obvious storage places:

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
   if (combo != null) 
       dataGridView1[e.ColumnIndex, e.RowIndex].Tag = combo.SelectedIndex;
}

在这个版本中,你显然会从 Tag 中检索索引,而不是从组合中..

In this version you will obviously retrieve the index from the Tag, not from combo..

当然你也可以像这样从 Value 中找到一个 Item:

Of course you could also find an Item from the Value like this:

DataGridViewComboBoxCell dcc = 
                        (DataGridViewComboBoxCell)dataGridView1[yourColumn, yourRow];
int index = dcc.Items.IndexOf(dcc.Value);

但这只会得到第一个拟合指数,而不是实际选择的指数..

But that will simply get the first fitting index, not the one that was actually chosen..

这篇关于获取 DatagridviewComboBoxCell 的 SelectedIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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