Set selectedindex for comboboxcell in datagridview vb.net

29.9k views Asked by At

I am trying to set the selectedindex for my datagridviewcomboboxcell through cellvaluechanged event,i.e when i change some value in my datagrid row, this column should get automatically changed. here's the code.

 Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

          Try       
                If Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString <> Nothing Then
                    Me.DataGridView1("unit_code", e.RowIndex).Value = "1" 
                    MsgBox(Me.DataGridView1.Rows(e.RowIndex).Cells(6).Value)
                End If
            Else
                Exit Sub
            End If

        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try 



my datagridview is bound to dataset. Searched many sites and did all research, got the above solution where we need to set the valuemember to .value part. Even after doing that it gives
system.formatexception:datagridviewcomboboxcell value is not valid.

Please help me.

[EDIT]
table structure
unit_code    descp
0                     -
1                     nos
2                     kgs
3                     gms

[EDIT 2]

 query = "select Switch(qty=0,'2',qty<=rcvd_qty,'1',rcvd_qty=0,'2',qty>rcvd_qty,'3') as Status,item_type,catalogue,name,pd.rate,qty,pd.unit_code" _
                & " from pur_det pd,itemhead th where pd.item_code=th.itemcode and order_no=0 order by catalogue"

            adap1 = New OleDbDataAdapter(query, conn)
            Dim saldet As New DataSet
            adap1.Fill(saldet, "puritems")

            Me.DataGridView1.DataSource = saldet.Tables(0)
            DataGridView1.Columns.Item(0).HeaderText = "STATUS"
            DataGridView1.Columns.Item(0).Width = 68
            DataGridView1.Columns.Item(1).HeaderText = "TYPE"
            DataGridView1.Columns.Item(1).Width = 60
            DataGridView1.Columns.Item(2).HeaderText = "CATALOGUE"
            DataGridView1.Columns.Item(2).Width = 140
            DataGridView1.Columns.Item(3).HeaderText = "DESCRIPTION"
            DataGridView1.Columns.Item(3).Width = 300
            DataGridView1.Columns.Item(4).HeaderText = "RATE"
            DataGridView1.Columns.Item(4).Width = 60
            DataGridView1.Columns.Item(5).HeaderText = "QUANTITY"
            DataGridView1.Columns.Item(5).Width = 84
            DataGridView1.Columns.Item(6).HeaderText = "UNIT" ' this column is removed below because it only had primary key values of this table("unitmast").
            DataGridView1.Columns.Item(6).Width = 70

            adap1 = New OleDbDataAdapter("select * from unitmast order by unitcode ASC", conn)
            Dim unitc As New DataSet
            adap1.Fill(unitc, "unitmast")

            Me.DataGridView1.Columns.RemoveAt(6) 'here the same is added with display member to view its actual value
            Dim uncol As New DataGridViewComboBoxColumn
            With uncol
                .Name = "unit_code"
                .DataPropertyName = "unit_code"
                .DataSource = unitc.Tables(0)
                .ValueMember = "unitcode"
                .DisplayMember = "desc"
                .HeaderText = "UNIT"
                .FlatStyle = FlatStyle.Flat
                .DropDownWidth = 160
                .Width = 70
            End With
            Me.DataGridView1.Columns.Insert(6, uncol)
3

There are 3 answers

2
Tea With Cookies On BEST ANSWER

A DataGridViewComboBoxCell has no SelectedIndex or SelectedValue property.

You can set the value directly like this:

CType(Me.DataGridView1("unit_code", e.RowIndex), DataGridViewComboBoxCell).Value = "your value string"

Or using the index of the items collection: (this works only if you have not set ValueMember and DisplayMember properties)

Dim combo As DataGridViewComboBoxCell
combo = CType(Me.DataGridView1("unit_code", e.RowIndex), DataGridViewComboBoxCell)
combo.Value = combo.Items('your index')

You can also check a value for nothing like this:

If Not Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value Is Nothing Then
    'Your code
End If
2
Dotnetter On

I Write and Test that Code For You Good Luck:

Dim Dt As New DataTable
Sub CreateDT()
    Dt.Columns.Add("Col1")
    Dt.Columns.Add("Col2")

    Dt.Rows.Add(New Object(1) {"1", "A"})
    Dt.Rows.Add(New Object(1) {"2", "B"})
    Dt.Rows.Add(New Object(1) {"3", "C"})
End Sub
Private Sub ChildRibbonForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    CreateDT()
    '
    'Dim CellCol As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(0), DataGridViewComboBoxColumn)

    Dim CellCol As New DataGridViewComboBoxColumn
    CellCol.Items.Add("Test1")
    CellCol.Items.Add("Test2")
    CellCol.Items.Add("Test3")

    Me.DataGridView1.DataSource = Dt
    Me.DataGridView1.Columns.Insert(0, CellCol)
    Dim CellBox As DataGridViewComboBoxCell = CType(DataGridView1.Rows(1).Cells(0), DataGridViewComboBoxCell)
    'Select the Second Item
    CellBox.Value = CellCol.Items(1)
End Sub
1
Nforndzi Ngen On
'Declare datatable
Dim cdt as new Datatable
'Add columns with types. It is important you specify the type for value member column
cdt.Columns.Add("vm", System.Type.GetType("System.Int32"))
cdt.Columns.Add("dm", System.Type.GetType("System.String"))

'Add items to table
cdt.Rows.Add
cdt.Rows(0).Item(0) = 1
cdt.Rows(0).Item(1) = "Red Cat"
cdt.Rows.Add
cdt.Rows(1).Item(0) = 2
cdt.Rows(1).Item(1) = "Black Cat"

'Add datasource to DataGridViewComboBoxColumn
Dim cc As DataGridViewComboBoxColumn
cc = datagridview1.Columns('colIndex')
cc.DataSource = cdt
cc.ValueMember = "vm"
cc.DisplayMember = "dm"

'Setting then selected value is thus:
Datagridview1.Rows('rowIndex').Cells('colIndex').Value = 2  '2 for black cat

This works for me