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