我有兩個連接的表單,第一個表單有一個 datagridview(DGV),它將顯示員工串列。
當我雙擊該行時,來自 FGV 的資料將轉換為第二種形式供用戶編輯資料。DGV 中的每一列都將插入文本框組合框等處。
但是當我雙擊 DGV 時,第二個表單上的組合框正在使用資料庫;其他的很好,但對于組合框,它將顯示資料庫中的第一個資料。
這是雙擊第一個表單的代碼:
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim frmEdit As New Edit
frmEdit.lblEENo.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
frmEdit.txtName.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString
frmEdit.txtAge.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString
frmEdit.lblAgeCategory.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString
frmEdit.txtGender.Text = DataGridView1.CurrentRow.Cells(5).Value.ToString
frmEdit.cbEthnicity.Text = DataGridView1.CurrentRow.Cells(6).Value.ToString
frmEdit.cbGrade.Text = DataGridView1.CurrentRow.Cells(7).Value.ToString
frmEdit.cbCategory.Text = DataGridView1.CurrentRow.Cells(8).Value.ToString
frmEdit.cbDepartment.Text = DataGridView1.CurrentRow.Cells(9).Value.ToString
frmEdit.cbPosition.Text = DataGridView1.CurrentRow.Cells(10).Value.ToString
frmEdit.txtReporting.Text = DataGridView1.CurrentRow.Cells(11).Value.ToString
frmEdit.DateTimePicker1.Value = DataGridView1.CurrentRow.Cells(12).Value.ToString
frmEdit.DateTimePicker2.Value = DataGridView1.CurrentRow.Cells(13).Value.ToString
If DataGridView1.CurrentRow.Cells(14).Value.ToString = "Y" Then
frmEdit.rbYes.Checked = True
ElseIf DataGridView1.CurrentRow.Cells(14).Value.ToString = "N" Then
frmEdit.rbNo.Checked = True
End If
frmEdit.cbStatus.Text = DataGridView1.CurrentRow.Cells(15).Value.ToString
frmEdit.txtNote.Text = DataGridView1.CurrentRow.Cells(16).Value.ToString
frmEdit.lblMody.Text = tslblLoginAs.Text
frmEdit.ShowDialog()
load_data()
End Sub
這是用于連接到組合框的資料庫表的第二種形式的代碼:
Private Sub Edit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim frmMasterList As New MasterStaff
Dim poscmd As New SqlCommand("SELECT * from MasterPositionList", conn)
Dim posadapt As New SqlDataAdapter(poscmd)
Dim postable As New DataTable
posadapt.Fill(postable)
cbPosition.DataSource = postable
cbPosition.DisplayMember = "PositionName"
Dim depcmd As New SqlCommand("SELECT * from MasterDepartmentList", conn)
Dim depadapt As New SqlDataAdapter(depcmd)
Dim deptable As New DataTable
depadapt.Fill(deptable)
cbDepartment.DataSource = deptable
cbDepartment.DisplayMember = "DeparmentName"
End Sub
uj5u.com熱心網友回復:
我想知道當您可以直接在 DGV 中編輯資料時,為什么需要單獨的表單。當您想到它時,您嘗試編輯的所有資料都已在 DGV 中找到(或應該存在于其資料源中)。您甚至可以在 DGV 中使用組合框,并帶有它們自己的系結資料表。這是一個示例,展示了如何做到這一點。
在 Edit_Load 中有不必要的重復。兩次加載相同的資料集。您可以重用它,甚至在啟動時加載它,如果它在應用程式運行時沒有更改,則將其保存在快取中。
另外,與其從 DGV 獲取值,不如獲取選定的資料行。為此,您只需要記錄 ID...無需訪問 UI 屬性,而是使用底層資料表。
你不應該使用索引號。如果您想插入列或移動列,想象一下可能會出現錯誤和潛在的錯誤。此外,用戶可能可以通過拖放重新排列 DGV 中的列......因此您的邏輯被破壞了,程式將不會像它應該的那樣運行。
無論如何,要回答你的問題:你已經加載了一個資料表,現在你要做的就是將它系結到組合(你做了)。您已經使用了 DataSource 和 DisplayMember 屬性。您可以使用 SelectedValue 在串列中預選一個值,如下所示:
With cbPosition
.DataSource = postable
.DisplayMember = "PositionName"
.ValueMember = "PositionName"
.SelectedValue = "The text value that comes from the DGV"
End With
只要來自 DGV 的值存在于資料表中,這應該有效。同樣,我認為您應該簡化您的 UI。所有資料都可以在 DGV 中就地編輯。
uj5u.com熱心網友回復:
表單加載事件觸發太晚而無法填充組合框。當您在DataGridView1_CellDoubleClick()方法中創建表單實體時,該事件幾乎立即引發,但在控制回傳到訊息傳遞回圈之前,事件處理程式實際上無法運行,并且在您之后的frmEdit.ShowDialog()行之前沒有機會發生這種情況已經設定了組合框值。因此 Load 事件在您填充所需的值后運行,并將覆寫該作業。
要解決此問題,請將 Load 代碼移動到建構式中,就在InitializeComponent()方法之后。
此外,由于您想從資料庫提供的專案中進行選擇,您應該考慮使用SelectedItemorSelectedValue屬性,而不是Text屬性。
最后,在整個應用程式或表單中重用相同的連接物件真的很糟糕。是的,資料庫連接是創建和管理的昂貴物件,但 ADO.Net 庫已經為您處理了這個問題。SqlConnection您在代碼中使用的物件是在更昂貴的實際原始連接池上的輕量級包裝器。當您嘗試重用一個時SqlConnection,您會在便宜的東西上獲得效率,而在更昂貴的東西上卻失去效率。
相反,每次需要使用資料庫時都創建(并及時Dispose()!)一個新SqlConnection物件,并考慮通過減少到資料庫的往返來減少連接。在這種情況下,您可以將兩個SELECT陳述句組合成一個字串,并在同一個資料庫行程中填充兩個 DataTable:
Private Sub New ' Replaces the current Edit_Load() method
InitializeComponent()
'Using block will make sure the connection is closed, **even if an exception is thrown**
' Do NOT(!) try to reuse the connection. Only reuse the connection string.
Using conn As New SqlConnection(connString), _
' Two SELECT queries in one string
cmd As New SqlCommand("SELECT * from MasterPositionList;SELECT * from MasterDepartmentList", conn), _
da As New DataAdapter(cmd)
Dim results As New DataSet
da.Fill(results)
cbPosition.DisplayMember = "PositionName"
cbDepartment.DisplayMember = "DeparmentName"
cbPosition.DataSource = results.Tables(0) 'Filled two tables in one database call
cbDepartment.DataSource = results.Tables(1)
End Using
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/345562.html
上一篇:面試基礎篇|作業系統|行程
