The CellClick DataGridView in the combobox does not appear in the vb.net.
is there something wrong in my code? I updated the code from me so that the answer can be adjusted in the code because I am still confused with the answer
thanks
'load database to datatable then to datagridview
Private Sub LoadData(Optional ByVal keyword As String = "")
Sql = "SELECT Auto_ID, First_Name, Last_Name, [First_Name] ' ' [Last_Name] AS Full_Name, Gender FROM TBL_SMART_CRUD " &
"WHERE [First_Name] ' ' [Last_Name] LIKE @keyword1 OR Gender = @keyword2 ORDER BY Auto_ID ASC"
Dim dt As DataTable = PerformCRUD(Cmd)
'update binding source
BindingSource1.DataSource = dt
With DataGridView1
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.AutoGenerateColumns = True
'update binding source
.DataSource = BindingSource1
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "First Name"
.Columns(2).HeaderText = "Last Name"
.Columns(3).HeaderText = "Full Name"
.Columns(4).HeaderText = "Gender"
End With
End Sub
'module AccessDb_Connection.vb
Public Function PerformCRUD(ByVal Com As OleDbCommand) As DataTable
Dim da As OleDbDataAdapter
Dim dt As New DataTable()
Try
da = New OleDbDataAdapter
da.SelectCommand = Com
da.Fill(dt)
Return dt
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Perform CRUD OPERATIONS Failed. : Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Error)
dt = Nothing
End Try
Return dt
End
End Function
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim dgv As DataGridView = DataGridView1
If e.RowIndex <> -1 Then
IDTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(0).Value).Trim()
UpdateButton.Text = "UPDATE (" & Me.ID & ")"
DeleteButton.Text = "DELETE (" & Me.ID & ")"
FirstNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value).Trim()
LastNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value).Trim()
GenderComboBox.SelectedItem = Convert.ToString(dgv.CurrentRow.Cells(4).Value).Trim()
End If
End Sub
String collection editor cellclick
CodePudding user response:
It appears that you are retrieving data from a database into a DataTable
and binding that to the DataGridView
. In that case, you should bind that same DataTable
to the individual controls. That way, selecting a row in the grid will automatically load that same row into the individual controls. You should also add a BindingSource
to your form and bind through that, if you aren't already. E.g.
thingBindingSource.DataSource = thingDataTable
thingDataGridView.DataSource = thingBindingSource
stuffTextBox.DataBindings.Add("Text", thingBindingSource, "Stuff")
When you add a binding to a control, the first argument is the name of the control property and the last is the name of the source column/property. In the case of a ComboBox
, you would normally bind data from another table and display a name/description and hide the ID, then have the ID populate a foreign key column in the other table. That might look like this:
With otherStuffComboBox
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = referenceListDataTable
.DataBindings.Add("SelectedValue", thingBindingSource, "ForeignKeyColumn")
End With
It sounds like you're not doing that though. It sounds like you just have a list of Strings
in the ComboBox
and they are populating the other table directly. In that case, you can bind to the SelectedItem
rather than the SelectedValue
.
By the way, if you want to save changes to the database, you should be saving all the changes from the DataTable
using the same data adapter that you used to populate it in the first place. Each time you edit a row, those changes are stored in the DataTable
. You can navigate around and edit as many rows as you like and all changes are stored. To delete, you call RemoveCurrent
on the BindingSource
and that change is stored too. You can then call Update
on a data adapter to save all the changes in a batch.