在datagridview中设置组合框的'SelectedIndex'值 [英] Set 'SelectedIndex' value of combobox in datagridview

查看:79
本文介绍了在datagridview中设置组合框的'SelectedIndex'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我不知道为什么这会如此困难,但花了几个小时搜索解决方案却没有成功。



我的表单包含一个只有三列的DataGridView(未绑定),最后一个是组合框列。我在设计视图中填充了Items集合,只有两个项目V和& P。



我希望组合框在表单加载时显示相应的项目,并使用如下代码。 Dgv3是DataGridView,dr是DataRow。





Hi
I am not sure why this should be so difficult but have spent hours googling for a solution without success.

My form contains a DataGridView (unbound) with only three columns, the last being a combobox column. I have populated the Items collection in design view with just two items "V" & "P".

I would like the combobox to display the appropriate item when the form loads, and have used code as follows. Dgv3 is the DataGridView and dr is a DataRow.


If dr.Item("RecPV").ToString) = "V" Then
    cbxIndex = 0
Else
    cbxIndex = 1
End If
Dgv3.Rows(i).Cells("DebitTo").Value = cbxIndex





我知道组合框单元不是一个真正的组合框,并且没有SelectedIndex属性,但肯定有一个简单的解决方案来完成这项任务。



非常感谢任何帮助。在此先感谢。



I am aware that a combobox cell is not a real combo box and doesn't have a SelectedIndex property but surely there is a easy solution to this task.

Any assistance would be greatly appreciated. Thanks in advance.

推荐答案

解决方案的关键是属性 System.Windows.Forms.DataGridViewComboBoxCell.Items 。请注意,这是 System.Object 类型元素的集合,这意味着绝对任何类型。不像许多人错误地认为的那样是弦乐,而是任何东西。请参阅:

http:// msdn .microsoft.com / zh-cn / library / system.windows.forms.datagridviewcomboboxcell.items.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.objectcollection.aspx [ ^ ],

http ://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxce ll.objectcollection.item.aspx [ ^ ]。



(一个令人困惑的时刻是:每个UI中显示的字符串是什么元件?答案很容易猜到:无论 ToString()返回。实际上,添加某些自定义类型的元素非常有用,可以获得与每个元素关联的数据的完整信息。因此,要控制每个元素在UI中的显示方式,只需在自定义元素类型中覆盖 System.Object.ToString()。)
由于数据不是绑定,最终你在代码中添加元素。您只需要保持对元素集合的访问。当您为属性 System.Windows.Forms.DataGridViewComboBoxCell.Value 分配一些值时,如果该值是您添加到组合框列表中的对象之一,则此值将被选中:

The key of the solution is the property System.Windows.Forms.DataGridViewComboBoxCell.Items. Pay attention, that this is a collection of the elements of the type System.Object, which means "absolutely any type". Not strings, as many mistakenly think, but anything. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.items.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.objectcollection.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.objectcollection.item.aspx[^].

(One confusing moment is: what string is shown in the UI for each element? The answer is easy to guess: whatever ToString() returns. In practice, it's very useful to add elements of some custom type, to have full information on the data associated with each element. So, to control how each element is shown in UI, just override System.Object.ToString() in your custom element type.)
As the data is not bound, eventually you add elements in your code. You just need to keep access to your element collection. When you assign some value to the property System.Windows.Forms.DataGridViewComboBoxCell.Value, and if the value is one of the objects you added to the combo box list, this value will be selected:
object[] myItems = new object[] { FirstElement, SecondElement, ThirdElement, }; // suppose you had some three objects
// representing the combo box list element; they can even be of different types, which does not have to be strings
myComboBoxCell.AddRange(myItems);

//...

myComboBoxCell.Value = SecondElement; // should cause selection of element #1

请尝试。



祝你好运,

-SA


这篇关于在datagridview中设置组合框的'SelectedIndex'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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