动态添加组合单元到datagridview

时间:2012-10-03 23:04:17

标签: vb.net datagridview datagridcomboboxcolumn datagridviewcombobox datagridviewcomboboxcell

我有一个动态填充的datagridview。我希望根据某些条件,可以使用组合框填充一些列。

但是我的组合框在最底行而不是正确的行中添加。

有人可以告诉我需要做什么吗?

我哪里出错了?

Private Sub dgvSteps_CellContentClick(ByVal sender As Object,
                                          ByVal e As DataGridViewCellEventArgs
                                          ) Handles dgvSteps.CellContentClick

        Dim reader = New XmlTextReader("C:\Qualcomm\" & tempNode.profilePath)
        reader.WhitespaceHandling = WhitespaceHandling.None
        reader.Read()
        reader.Read()
        reader.Read()
        dgv2.Rows.Clear()


        While reader.NodeType <> XmlNodeType.EndElement
            Dim str1 As String
            Dim str2 As String
            str1 = reader.Name
            str2 = reader.ReadElementString(str1)

            If reader.Name = "Port" Then
                Dim dgv2Cb As New DataGridViewComboBoxCell  'Create DatagridViewComboBoxCell
                Dim ports As String() = SerialPort.GetPortNames()
                Dim port As String
                For Each port In ports
                    dgv2Cb.Items.Add(port)
                Next port
                dgv2Cb.Sorted = True
                dgv2.Rows.Add(str1, str2)
                rowIndex = dgv2.RowCount  'Get the RowCount at the time of adding combobexcell, and add comboboxcell in that row

                dgv2.Rows(rowIndex).Cells(1) = dgv2Cb
            Else
                dgv2.Rows.Add(str1, str2)
                               End If
        End While
    End Sub

1 个答案:

答案 0 :(得分:0)

您是否将datagrid的AutoGenerateColumns属性设置为false?

dgCFG.AutoGenerateColumns = false;