0

I get the following error:

System.InvalidCastException: 'Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.'

In this procedure:

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

When I change the following procedure's name:

Private Sub DgdUsersList_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.SelectionChanged
    If e.RowIndex >= 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

From:

Private Sub DgdUsersList_CellClicked(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.CellClicked

To:

Private Sub DgdUsersList_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.SelectionChanged

The scenario is this; I have two Datagridview controls and I want to make it so that when I click on any cell in DgdEmployeesList, the same row (index) is selected in DgdEmployeesList.

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)

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)

Any help is greatly appreciated!

James

James
  • 69
  • 1
  • 9
  • `SelectionChanged` has the second argument as `EventArgs`, not as `DataGridViewCellEventArgs`. Fix the definition and the problem goes away. – Sami Kuhmonen Jun 28 '17 at 17:11
  • This removes that error, but still _**does not**_ solve the problem - I want to make it so that when I select a row inside `DgdEmployeesList`, the same row/index is selected _automatically_ inside `DgdUsersList`. All this does is remove that error, but also removes the ability to get the row index from `DgdUsersList` – James Jun 28 '17 at 18:07
  • @James Within the `SelectionChanged` handler you're going to need to query the `DgdEmployeesList.CurrentCell.RowIndex` property as [you can see here](https://stackoverflow.com/questions/3578144/index-of-currently-selected-row-in-datagridview), then set the `DgdUsersList` selected row to that index. There's many other way to go about this as well, just start digging around. – JSteward Jun 28 '17 at 21:07

0 Answers0