![](/img/trans.png)
[英]Combobox population does not appear in DataGridView in vb.net
[英]The CellClick DataGridView in the combobox does not appear in the vb.net
combobox 中的 CellClick DataGridView 不會出現在 vb.net 中。
我的代碼有問題嗎? 我更新了我的代碼,以便可以在代碼中調整答案,因為我仍然對答案感到困惑
謝謝
'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
您似乎正在將數據從數據庫檢索到DataTable
並將其綁定到DataGridView
。 在這種情況下,您應該將同一個DataTable
綁定到各個控件。 這樣,在網格中選擇一行將自動將同一行加載到各個控件中。 如果您還沒有,您還應該將BindingSource
添加到您的表單並通過它進行綁定。 例如
thingBindingSource.DataSource = thingDataTable
thingDataGridView.DataSource = thingBindingSource
stuffTextBox.DataBindings.Add("Text", thingBindingSource, "Stuff")
將綁定添加到控件時,第一個參數是控件屬性的名稱,最后一個參數是源列/屬性的名稱。 在ComboBox
的情況下,您通常會綁定來自另一個表的數據並顯示名稱/描述並隱藏 ID,然后讓 ID 填充另一個表中的外鍵列。 這可能看起來像這樣:
With otherStuffComboBox
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = referenceListDataTable
.DataBindings.Add("SelectedValue", thingBindingSource, "ForeignKeyColumn")
End With
聽起來你沒有這樣做。 聽起來您在ComboBox
中只有一個Strings
列表,它們直接填充另一個表。 在這種情況下,您可以綁定到SelectedItem
而不是SelectedValue
。
順便說一句,如果您想保存對數據庫的更改,您應該使用最初用於填充數據表的相同數據適配器保存DataTable
中的所有更改。 每次編輯一行時,這些更改都會存儲在DataTable
。 您可以隨意瀏覽和編輯任意數量的行,所有更改都會被存儲。 要刪除,您可以在BindingSource
上調用RemoveCurrent
,並且該更改也會被存儲。 然后,您可以在數據適配器上調用Update
以批量保存所有更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.