Data Gridview自动增量

时间:2016-07-15 11:56:15

标签: vb.net visual-studio-2010 datagridview

任何人都可以为我修改此代码。

        Private Sub DGV_Items_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Items.CellClick
    Dim rowIndex As Integer = e.RowIndex
    Dim celval As String
    With DGV_Items
        If DGV_Items.Rows.Count > 1 Then
            celval = .Rows(.RowCount - 2).Cells(.ColumnCount - 4).Value
        End If
    End With

    If rowIndex = 0 Then

        cmd = New SqlCommand("Select S_No from Orders where S_No = (Select MAX(S_No) from Orders)", con)
        If con.State = ConnectionState.Closed Then con.Open()
        dr = cmd.ExecuteReader()
        While dr.Read()
            result = dr.Item("S_No")
        End While
        dr.Close()
        If con.State = ConnectionState.Open Then con.Close()
        DGV_Items.Rows(0).Cells(0).Value = (result + 1).ToString

    ElseIf DGV_Items.CurrentRow.Cells(0).Value Is Nothing Then

        If rowIndex > 0 Then
            DGV_Items.CurrentRow.Cells(0).Value = celval + 1
        End If
    End If
End Sub

除非其中一个单元格(单元格(1)或单元格(2))包含值(字符串),否则我不希望分配ID。

我尽力解决但却无法解决现在的阶段需要2天,我认为编写这样的代码也不错。

谢谢。

0 个答案:

没有答案