在datagridview vb.net中为comboboxcell设置selectedindex

时间:2014-11-13 12:27:38

标签: vb.net datagridview datagridviewcomboboxcell

我正在尝试为selectedindexdatagridviewcomboboxcell事件设置cellvaluechanged,即当我更改数据网格行中的某个值时,此列应自动更改。这是代码。

 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 


我的datagridview绑定到数据集。搜索了许多网站并进行了所有研究,得到了上述解决方案,我们需要将valuemember设置为.value部分。即使这样做,它也会给system.formatexception:datagridviewcomboboxcell value is not valid

请帮帮我。

[编辑]
表结构
unit_code descp
0 -
1号 2公斤 3克
[编辑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)

4 个答案:

答案 0 :(得分:4)

DataGridViewComboBoxCell没有SelectedIndexSelectedValue属性。

您可以直接设置值:

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

或者使用items集合的索引:(仅当您未设置ValueMemberDisplayMember属性时才有效)

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

你也可以检查一下这样的值:

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

答案 1 :(得分:2)

我为你编写和测试代码祝你好运:

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

答案 2 :(得分:0)

'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

这对我有用

答案 3 :(得分:0)

看这里可能是一个很好的解决方案。

Useful linck here