Question: I have 1 DataGridView with 3 columns, 1 button named as **AssignID**, 1 Database with **EmployeeList** Table.

Columns of Datagridview

1. Column 1 = EmployeeID
2. Column 2 = EmployeeName
3. Column 3 = Department

I'm pasting the data of **EmployeeName** and **Department** from excel into DataGridView leaving column 1 as blank. My problem is how to automatically assign the **EmployeeID** per Employee in the DataGridView when I press the button **AssignID**. The last **IDNumber** in the database is 10000.

Appreciate your help. Im still learning vb.net. Thanks in Advance.

Im using below 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

```

Answer: 
An `int` is not a `bool`. Try it and find out what happens (InvalidCastException).

![InvalidlCastException thrown](https://i.stack.imgur.com/pVMPM.png)

One should note that most of the primitive types explicitly implement `IConvertible`, so you can get the cast you want this way:

```
int    a = 37 ;
object b = a ;
bool   c = (b as IConvertible) != null && (b as IConvertible).ToBoolean(null);

```

In the case of an `int` → `bool` conversion like this, the conversion is done in the usual C-like manner: zero is `false` and non-zero is `true`. Worth noting that this behaviour is at odds with formal logic wherein falsity is multi-valued, being all that is not true and truth uni-valued. But I digress.

As far as custom conversions go, whether implicit or explicit, all you're doing is providing custom methods that are recognized as operators by the compiler. The *how* of the conversion, of course, is up to you and your implementation. Here, the class `Foo` knows how to convert itself [explicitly] to an instance of class `Bar`:

```
class Foo
{
  public static explicit operator Bar(Foo x)
  {
    // your implementation here
    throw new NotImplementedException();
  }
}

```

And here, class `Bar` knows how to [explicitly] convert itself to an `int` and to [implicitly] convert itself to a `Foo`:

```
class Bar
{
    public static explicit operator int(Bar x)
    {
        // your implementation here
        throw new NotImplementedException();
    }
    public static implicit operator Foo(Bar x)
    {
        // your implementation here
        throw new NotImplementedException();
    }
}

```

When you start mixing stuff up, the compiler will figure out what conversion method(s) are applicable (or not) and resolve them. You'll either get a compile error about the conversion or the compile will have selected a conversion method to be invoked at runtime.

At runtime, of course, it is possible that an exception might be thrown in the course of the conversion.
Probably your best course of action to reduce exceptions and make casting easy is to use the [as](http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx) operator and a [nullable](http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx) boolean `bool?`.

```
object test1 = 1;
object test2 = true;

var testBool1 = test1 as bool?;
var testBool2 = test2 as bool?;

if (testBool1.HasValue) 
{
  // test1 is a boolean
  bool mybool = testBool1.Value;
}

if (testBool2.HasValue)
{
  // test2 is a boolean
  bool mybool = testBool2.Value;
}

```