title: VB.NET - System.InvalidCastException (Datagridview)
body: <p>I get the following error:</p>
  
  <p>System.InvalidCastException: 'Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.'</p>
  
  <p>In this procedure:</p>
  
  <pre><code>Private Sub FillUsersGrid()
      Dim sql As String = "SELECT * FROM Users"
      Dim dataadapter As New SqlDataAdapter(sql, SQLconn)
      Dim Users As New DataSet()
      SQLconn.Open()
      dataadapter.Fill(Users, "Users_table")
      SQLconn.Close()
      DgdUsersList.DataSource = Users
      DgdUsersList.DataMember = "Users_table"
      Me.DgdUsersList.AutoResizeColumns()
  End Sub
  </code></pre>
  
  <p>When I change the following procedure's name:</p>
  
  <pre><code>Private Sub DgdUsersList_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.SelectionChanged
      If e.RowIndex &gt;= 0 Then
          Row = Me.DgdUsersList.Rows(e.RowIndex)
          TxtEmployeesUsername.Text = Row.Cells("Username").Value.ToString
          TxtEmployeesPassword.Text = Row.Cells("Password").Value.ToString
          CmbEmployeesAccessLevel.Text = Row.Cells("AccessLevel").Value
          ChkEmployeesPOSAccess.Checked = Row.Cells("POSAccess").Value
          CmbEmployeesPOSPosition.Text = Row.Cells("POSPosition").Value.ToString
      End If
      DgdUsersList.Rows(CurrentRowIndex).Selected = True
  End Sub
  </code></pre>
  
  <p>From:</p>
  
  <p><code>Private Sub DgdUsersList_CellClicked(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.CellClicked</code></p>
  
  <p>To:</p>
  
  <p><code>Private Sub DgdUsersList_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.SelectionChanged</code></p>
  
  <p>The scenario is this; I have two Datagridview controls and I want to make it so that when I click on any cell in <code>DgdEmployeesList</code>, the same row (index) is selected in <code>DgdEmployeesList</code>.</p>
  
  <p>This is because I want to populate a form with a record when a record is clicked, and the employees table in the SQL database is linked to the users one in the same database via a primary key (ID - When an employee profile is created, a user profile is created with the same ID using the same form)</p>
  
  <p>Is there any fix for this (I understand it may be easier to code this just by using the datagridview control itself, but I am unaware of a way to make two tables display inside the control and how to separate them upon saving, along with editing records and deleting them)</p>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>James</p>