Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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

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

            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()

                    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()
                    ComboBox2.ValueMember = "Item_Make"
                    ComboBox2.DisplayMember = "Item_Make"

                    ComboBox2.DataSource = dtItem
                End Using
            End Using
        End Using

    End Sub
Updated 6-May-16 15:35pm

1 solution

Think by yourself: if someone decided to give a property Text 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 that Text property from nowhere, quite naturally, there is no such thing; so this is just your fantasy.

The property SelectedIndex of this class is also your fantasy, perhaps a bit less unreasonable than the Text 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)[^];
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 property Value of the type System.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 property ValueType. 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 type System.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.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900