title: Automatically Assign Employee Number In Datagridview
body: <p>I have 1 DataGridView with 3 columns, 1 button named as <strong>AssignID</strong>, 1 Database with <strong>EmployeeList</strong> Table.</p>
  
  <p>Columns of Datagridview</p>
  
  <ol>
  <li>Column 1 = EmployeeID   </li>
  <li>Column 2 = EmployeeName   </li>
  <li>Column 3 = Department   </li>
  </ol>
  
  <p>I'm pasting the data of <strong>EmployeeName</strong> and <strong>Department</strong> from excel into DataGridView leaving column 1 as blank. My problem is how to automatically assign the <strong>EmployeeID</strong> per Employee in the DataGridView when I press the button <strong>AssignID</strong>. The last <strong>IDNumber</strong> in the database is 10000.</p>
  
  <p>Appreciate your help. Im still learning vb.net. Thanks in Advance.</p>
  
  <p>Im using below code:</p>
  
  <pre><code>Private Sub AssignID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AssignID.Click
      Dim Dbcon As New OleDbConnection(connStr)
  
      Dbcon.Open()
  
      Dim query As String
      Dim dbup As New OleDbCommand
      Try
  
          query = "SELECT Max (EmployeeID) from EmployeeList"
          dbup = New OleDbCommand(query, Dbcon)
  
          Dim EmpID As Integer
  
          EmpID = dbup.ExecuteScalar
  
          Dim i2 As Integer
          i2 = Me.NewEmployeesDataGridview.CurrentRow.Index
  
          NewEmployeesDataGridview.Item(0, i2).Value() = EmpID
  
          For x As Integer = 0 To NewEmployeesDataGridview.Rows.Count - 2
              NewEmployeesDataGridview.Rows(x).Cells(0).Value = NewEmployeesDataGridview.Rows(0).Cells(0).Value + 1
  
          Next
  
          NewEmployeesDataGridview.AllowUserToAddRows = False
  
      Catch ex As Exception
          MsgBox(ex.Message)
      End Try
  
  End Sub
  </code></pre>