依赖datagridview组合框列 [英] Dependent datagridview combo box column
问题描述
我有一个datagridview,其中包含一个表单中的两个组合框列和一个在数据库中有两列的表。我已将column1绑定到组合框1,将column2绑定到组合框2。当我选择combobox1时,我想将相应的行数据显示到combobox2中。我可以在常规组合框中执行此操作。当我放置代码时,我得到错误 -
Text不是'System.Windows.DatagridviewComboboxColumn'的成员
SelectedIndex不是System.Windows.DatagridviewComboboxColumn <的成员br />
有什么办法吗?任何信息都会有所帮助
谢谢
我尝试了什么:
I have a datagridview with two combobox column in a form and a table with two columns in database. I have bound column1 to combo box1 and column2 to combo box2. I want to display corresponding row data into combobox2 when I select combobox1. I can do it in regular combo box. When I place the code I get error-
Text is not a member of 'System.Windows.DatagridviewComboboxColumn'
SelectedIndex is not a member of System.Windows.DatagridviewComboboxColumn
Is there any way to do it? any bit of info would be helpful
Thanks
What I have tried:
Public Class Form1
Private Sub comboBoxName_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrEmpty(combobox1.Text) Then
fillMake()
Combobox2.SelectedIndex = -1
End If
End Sub
Private Sub fillName()
Dim str As String = "Select distinct Item_Name from Item"
Using con As New SqlConnection(" Data Source=USER-D501X28NCS\SQLEXPRESS;Initial Catalog=master;Integrated Security=True")
Using cmd As New SqlCommand(str, con)
Using adp As New SqlDataAdapter(cmd)
Dim dtItem As New DataTable()
adp.Fill(dtItem)
ComboBox1.DataSource = dtItem
ComboBox1.DisplayMember = "Item_Name"
ComboBox1.ValueMember = "Item_Name"
End Using
End Using
End Using
End Sub
Private Sub fillMake()
Using con As New SqlConnection(" Data Source=USER-D501X28NCS\SQLEXPRESS;Initial Catalog=master;Integrated Security=True ")
Dim str As String = "Select Item_Make from Item Where Item_Name=@Item_Name"
Using cmd As New SqlCommand(str, con)
' cmd.Parameters.AddWithValue("@Item_Name", ComboBox1.Text)
Using adp As New SqlDataAdapter(cmd)
Dim dtItem As New DataTable()
adp.Fill(dtItem)
ComboBox2.ValueMember = "Item_Make"
ComboBox2.DisplayMember = "Item_Make"
ComboBox2.DataSource = dtItem
End Using
End Using
End Using
End Sub
推荐答案
自己思考:如果有人决定将一个属性Text
提供给代表一列的类,它可能意味着什么?标题文字?不,它没有任何意义,因为表示标题的适当类表示单元类;它只是一种细胞。所以,你从不知道那个Text
属性的想法,很自然地,没有这样的东西;所以这只是你的幻想。
这个类的属性SelectedIndex
也是你的幻想,也许有点比Text
属性更不合理。再想一想:列的这种属性可以表示什么样的选择?未选择列。根据选项,您可以选择单个单元格,一组单元格,整行,一组行或列。但是所选列的索引没有任何意义,只是因为它可能是一组列。这样的财产是多余的,所以没有这样的东西。
所以,我的建议很简单:不要尝试一些不存在的房产或其他成员你只是幻想着,阅读原始的MSDN文档,然后按照它。至少,使用VS Intellisense。如果你这样做,你很容易(很容易)找出如何做出选择:
DataGridView.SelectedCells Property(System.Windows.Forms) [ ^ ],
DataGridView.SelectedRows Property(System.Windows) .Forms) [ ^ ],
DataGridView.SelectedColumns属性(System.Windows.Forms) [ ^ ];
选项:
DataGridView .SelectionMode属性(System.Windows.Forms) [ ^ ],
DataGridView.MultiSelect Property(System.Windows.Forms) [ ^ ];
另见:
DataGridView类(System.Windows.Forms) [ ^ ]。
使用text,事情有点棘手:你必须使用适用于所有单元格的无类型方法,使用类型System.Object
的属性值
但此属性值的运行时类型可以有所不同:有时它是字符串,有时是其他东西。您可以使用其属性ValueType
查询任何单元格的实际类型。请参阅:
DataGridViewCell.Value属性(System.Windows.Forms) [ ^ ],
DataGridViewCell.ValueType属性(System.Windows.Forms) [ ^ ] 。
这是一个想法:从类型System.Windows.Forms.DataGridViewCell
。您可以使用自己的值类型实现自己的类型;那么你必须相应地实现上面引用的两个属性。请参阅: DataGridViewCell类(System.Windows。表格) [ ^ ]。
这并不难。您只需阅读MSDN并按预期使用UI类及其成员。
-SA
Think by yourself: if someone decided to give a propertyText
to a class representing a column, what would it possibly mean? A header text? No, it would not make any sense, because the adequate class representing a header represent cell class; it's just the kind of a cell. So, you get the idea of thatText
property from nowhere, quite naturally, there is no such thing; so this is just your fantasy.
The propertySelectedIndex
of this class is also your fantasy, perhaps a bit less unreasonable than theText
property. Again, think a bit: what kind of selection could be represented by such property of a column? A column is not selected. Depending on options, you can make selectable a single cell, a set of cells, a whole row, a set of rows or columns. But the index of selected column would not make any sense, just because it could be a set of columns. Such property would be redundant, so there is no such thing.
So, my advice is quite simple: instead of trying out some non-existing properties or other member which you just fantasized out, read original MSDN documentation and simply follow it. At least, use VS Intellisense. If you did it, you would easily (very easily) find out how selection is made:
DataGridView.SelectedCells Property (System.Windows.Forms)[^],
DataGridView.SelectedRows Property (System.Windows.Forms)[^],
DataGridView.SelectedColumns Property (System.Windows.Forms)[^];
options:
DataGridView.SelectionMode Property (System.Windows.Forms)[^],
DataGridView.MultiSelect Property (System.Windows.Forms)[^];
see also:
DataGridView Class (System.Windows.Forms)[^].
With "text", things are a bit trickier: you have to use untyped approach applicable to all cells, using the propertyValue
of the typeSystem.Object
but the runtime type of this properties value can be different: sometime it's string, sometimes something else. You can query actual type of any cell using its propertyValueType
. Please see:
DataGridViewCell.Value Property (System.Windows.Forms)[^],
DataGridViewCell.ValueType Property (System.Windows.Forms)[^].
This is the idea: there are difference cell types derived from the typeSystem.Windows.Forms.DataGridViewCell
. You can implement your own type with your own value type; then you have to implement the two properties referenced above accordingly. Please see: DataGridViewCell Class (System.Windows.Forms)[^].
It's not really hard. You just need to read MSDN and use the UI classes and their member as intended.
—SA
这篇关于依赖datagridview组合框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!