组合框行为我很难理解 [英] combobox behaviour I'm struggling to understand

查看:47
本文介绍了组合框行为我很难理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单frmUpdateData上有几个组合框,一个文本字段和复选框。

组合框列表项是从数据库表中预先填充的。


这些控件中显示的值是通过从另一个表单上的datagridview传递值来设置的。

 frmUpdateData.ctlValues(DV1.CurrentRow .Cells(0).Value.ToString,_ 
DV1.CurrentRow.Cells(1).Value.ToString,_
DV1.CurrentRow.Cells(2).Value.ToString,_
DV1.CurrentRow.Cells(3).Value.ToString,_
DV1.CurrentRow.Cells(5).Value.ToString,_
DV1.CurrentRow.Cells(6).Value.ToString)

所有值都是从dgv中提取并正确传递给frmUpdateData


但在某些情况下,其中一个组合框文本是空白。 (通常它显示来自另一个表单传递的值的selectedItem)


进一步调查显示组合框中的所选项目是正确的,但它只是没有显示。 />
它始终是相同的组合框但只发生一定时间。

我可以解决问题

 CboJob.Text = CboJob.SelectedValue.ToString 


执行此操作的正确方法是什么,或者在此特定组合框中设置错误。

看起来与另一个相同。


解决方案

从DataGridView获取数据的常用方法是DataGridView有一个DataSource,例如BindingSource,其中BindingSource DataSource是DataSet(实际的源是DataSet中的DataTable)或DataTable。  ;


这允许你将someBindingSource.Current转换为DataRow,它允许你o获取数据。


示例


Dim ContactName As String = CType(someBindingSource.Current,DataRowView).Row.Field(Of String)(" ; ContactName")


或者第一个casy


Dim row As DataRow =  CType(someBindingSource.Current,DataRowView).Row


然后获取该行的每个字段


Dim ContactName As String = row.Field(Of String)(" ContactName")


Dim phoneNumber As String = row.Field(Of String)(" ContactPhone")


Dim joinDate As Date = row.Field(Of DateTime)(" JoindDate" )


当然,如果没有DataSource你可以做


someDataGridView。 EndEdit 然后尝试获取值。


I have a couple of combo boxes, a text field and checkbox on a form frmUpdateData.
The combobox listitems are pre-filled from a database table.

The values to display in these controls is set by passing values from a datagridview on another form.

frmUpdateData.ctlValues(DV1.CurrentRow.Cells(0).Value.ToString, _
                            DV1.CurrentRow.Cells(1).Value.ToString, _
                            DV1.CurrentRow.Cells(2).Value.ToString, _
                            DV1.CurrentRow.Cells(3).Value.ToString, _
                            DV1.CurrentRow.Cells(5).Value.ToString, _
                            DV1.CurrentRow.Cells(6).Value.ToString)

All the values are being extracted from the dgv and passed correctly to frmUpdateData

However in some circumstances one of the combo boxes text is blank. (Normally it shows selectedItem which comes from the value passed from the other form)

Further investigation shows that the selected item in the combo box is correct, but it's just not being displayed.
It is always the same combo box but only happens a certain times.
I can get round the problem by

CboJob.Text = CboJob.SelectedValue.ToString

What is the correct way of doing this or have I got some setting wrong on this particular combo box.
It appears identical to the other one.

解决方案

The common way to obtain data from a DataGridView is where the DataGridView has a DataSource such as a BindingSource where the BindingSource DataSource is a DataSet (where the actual source is a DataTable in the DataSet) or DataTable. 

This allows you to cast someBindingSource.Current to a DataRow which allows you to obtain data.

Example

Dim ContactName As String = CType(someBindingSource.Current,DataRowView).Row.Field(Of String)("ContactName")

Or first casy

Dim row As DataRow = CType(someBindingSource.Current,DataRowView).Row

Then get each field for the row

Dim ContactName As String = row.Field(Of String)("ContactName")

Dim phoneNumber As String = row.Field(Of String)("ContactPhone")

Dim joinDate As Date = row.Field(Of DateTime)("JoindDate")

Of course if there is no DataSource you could do

someDataGridView.EndEdit first then try and get the values.


这篇关于组合框行为我很难理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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