在 datagridview vb.net 中为组合框单元设置 selectedindex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26908886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:05:05  来源:igfitidea点击:

Set selectedindex for comboboxcell in datagridview vb.net

vb.netdatagridviewdatagridviewcomboboxcell

提问by Steve

I am trying to set the selectedindexfor my datagridviewcomboboxcellthrough cellvaluechangedevent,i.e when i change some value in my datagrid row, this column should get automatically changed. here's the code.

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

 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 valuememberto .valuepart. Even after doing that it gives
system.formatexception:datagridviewcomboboxcell value is not valid.



我的 datagridview 绑定到数据集。搜索了许多站点并进行了所有研究,得到了上述解决方案,我们需要将其设置valuemember.value部分。即使在这样做之后,它也会给
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]

请帮我。

[编辑]
表结构
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)

回答by Tea With Cookies

A DataGridViewComboBoxCellhas no SelectedIndexor SelectedValueproperty.

ADataGridViewComboBoxCell没有SelectedIndexSelectedValue财产。

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 ValueMemberand DisplayMemberproperties)

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

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

回答by Dotnetter

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

回答by Nforndzi Ngen

'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

这对我有用

回答by Amazigh.Ca

Look here it can be good solution.

看这里它可以是很好的解决方案。

Useful linck here

有用的链接在这里