如何通过在另一列中添加代码来在datagridview组合框中设置值

时间:2019-05-18 08:40:11

标签: vb.net

我设计了一个Windows窗体,在此窗体中,我正在使用具有4列的datagridview。在第二列中,我将列类型设置为组合框。在此组合框中,通过数据源添加了项。我想向用户提供从组合框选择名称的选项,或者如果他知道代码,则在第一列中输入代码,名称将自动显示在第二列中。我的问题是,如果用户从组合框中选择名称,那么一切都会顺利进行,但是如果他在第一列中输入代码并按Enter,则datagridview会引发数据错误。请帮忙。

Private Sub FrmVoucher_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Call Disable_fields(Me)
    Me.TextBox1.Focus()

    Dim ls As String

    ls = " select A_CODE,A_NAME FROM MASTERACCOUNTS ORDER BY A_NAME"

    Dim CMD As New SqlCommand(ls, TimeCn)
    Dim Da As New SqlDataAdapter(CMD)
    Dim table As New DataTable
    Da.Fill(table)

    Me.Ac_name.DisplayMember = "a_name"
    Me.Ac_name.ValueMember = "a_code"
    Me.Ac_name.DataSource = table



End Sub

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    ' On Error Resume Next
    Select Case e.ColumnIndex
        Case 0
            DataGridView1.CurrentCell = DataGridView1(2, DataGridView1.CurrentCell.RowIndex)
            DataGridView1.Rows(e.RowIndex).Cells(1).Value = GetAcName(DataGridView1.Rows(e.RowIndex).Cells(0).Value)
            SendKeys.Send("{UP}")
        Case 1
            DataGridView1.CurrentCell = DataGridView1(2, DataGridView1.CurrentCell.RowIndex)
            DataGridView1.Rows(e.RowIndex).Cells(0).Value = DataGridView1.Rows(e.RowIndex).Cells(1).Value
            SendKeys.Send("{UP}")


        Case 2
            If DataGridView1.Rows(e.RowIndex).Cells(2).Value = "D" Or DataGridView1.Rows(e.RowIndex).Cells(2).Value = "d" Or DataGridView1.Rows(e.RowIndex).Cells(2).Value = "C" Or DataGridView1.Rows(e.RowIndex).Cells(2).Value = "c" Then
                DataGridView1.CurrentCell = DataGridView1(3, DataGridView1.CurrentCell.RowIndex)
                SendKeys.Send("{UP}")
            Else
                DataGridView1.CurrentCell = DataGridView1(2, DataGridView1.CurrentCell.RowIndex)
                SendKeys.Send("{UP}")
            End If
        Case 3
            DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.CurrentCell.RowIndex)
            Call UpdateBalance()
    End Select

End Sub

当我将代码放在第一列中时,第二列(组合框)中没有显示任何值。没有显示错误。

0 个答案:

没有答案