組合框中的 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
字串集合編輯器 cellclick
uj5u.com熱心網友回復:
您似乎正在將資料從資料庫檢索到 aDataTable并將其系結到DataGridView. 在這種情況下,您應該將其系結DataTable到各個控制元件。這樣,在網格中選擇一行將自動將同一行加載到各個控制元件中。如果您還沒有,您還應該將 a 添加BindingSource到您的表單并通過它進行系結。例如
thingBindingSource.DataSource = thingDataTable
thingDataGridView.DataSource = thingBindingSource
stuffTextBox.DataBindings.Add("Text", thingBindingSource, "Stuff")
將系結添加到控制元件時,第一個引數是控制元件屬性的名稱,最后一個引數是源列/屬性的名稱。在 a 的情況下ComboBox,您通常會系結來自另一個表的資料并顯示名稱/描述并隱藏 ID,然后讓 ID 填充另一個表中的外鍵列。這可能看起來像這樣:
With otherStuffComboBox
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = referenceListDataTable
.DataBindings.Add("SelectedValue", thingBindingSource, "ForeignKeyColumn")
End With
聽起來你沒有這樣做。聽起來你只有一個串列,Strings它們ComboBox直接填充另一個表。在這種情況下,您可以系結到SelectedItem而不是SelectedValue.
順便說一句,如果您想保存對資料庫的更改,您應該DataTable使用最初用于填充資料庫的相同資料配接器保存所有更改。每次編輯行時,這些更改都會存盤在DataTable. 您可以隨意瀏覽和編輯任意數量的行,所有更改都會被存盤。要洗掉,您呼叫RemoveCurrent并且BindingSource該更改也被存盤。然后,您可以呼叫Update資料配接器以批量保存所有更改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511609.html
下一篇:Webhook接收器回應
