[英]The CellClick DataGridView in the combobox does not appear in the vb.net
The CellClick DataGridView in the combobox does not appear in the vb.net. combobox 中的 CellClick DataGridView 不会出现在 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
It appears that you are retrieving data from a database into a DataTable
and binding that to the DataGridView
.您似乎正在将数据从数据库检索到
DataTable
并将其绑定到DataGridView
。 In that case, you should bind that same DataTable
to the individual controls.在这种情况下,您应该将同一个
DataTable
绑定到各个控件。 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.如果您还没有,您还应该将
BindingSource
添加到您的表单并通过它进行绑定。 Eg例如
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.在
ComboBox
的情况下,您通常会绑定来自另一个表的数据并显示名称/描述并隐藏 ID,然后让 ID 填充另一个表中的外键列。 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.听起来您在
ComboBox
中只有一个Strings
列表,它们直接填充另一个表。 In that case, you can bind to the SelectedItem
rather than the SelectedValue
.在这种情况下,您可以绑定到
SelectedItem
而不是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.顺便说一句,如果您想保存对数据库的更改,您应该使用最初用于填充数据表的相同数据适配器保存
DataTable
中的所有更改。 Each time you edit a row, those changes are stored in the DataTable
.每次编辑一行时,这些更改都会存储在
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.要删除,您可以在
BindingSource
上调用RemoveCurrent
,并且该更改也会被存储。 You can then call Update
on a data adapter to save all the changes in a batch.然后,您可以在数据适配器上调用
Update
以批量保存所有更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.