我創建DataGridView1并填充它,以便每個單元格都是一個 ComboBox,其中的資料ComboNames()是文本檔案中的名稱串列。在主表單代碼中,我宣告了一個名為的全域陣列DropDownArray(),它是一個副本,ComboNames()因此我可以在其他 Subs 中使用它。當我測驗/除錯時,表單加載并且我可以在表單中做任何事情而不會出錯(有一堆按鈕和一個單獨的串列框)。但是,一旦我更改網格中的單元格值,它就會中斷。我的代碼后底部的錯誤訊息。希望我不會嘗試做一些無法完成的事情,因為我已經在這個專案上投入了很多時間
DataGridView 設定的片段:
DataGridView1.ColumnCount = 12
DataGridView1.RowCount = 12
DataGridView1.RowHeadersWidth = 50
For row As Integer = 0 To 11
DataGridView1.Rows(row).HeaderCell.Value = (row 1).ToString()
For col As Integer = 0 To 11
DataGridView1(col, row) = New DataGridViewComboBoxCell
Next
Next
組合框設定的片段:
Dim combo As DataGridViewComboBoxCell
For row = 0 To DataGridView1.Rows.Count - 1
For col = 0 To DataGridView1.Columns.Count - 1
combo = DataGridView1(col, row)
combo.DataSource = ComboNames
Next
Next
Started = 1
我嘗試更新未更改的單元格以具有更新名稱串列()的新組合框資料源失敗:
Private Sub DataGridView1_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Dim UpdatedNameList As New List(Of String)()
UpdatedNameList = DropDownArray '!!! DropDownArray() = ComboNames() !!!
If started = 1 Then
If DataGridView1.CurrentCell.Value.ToString.Length > 4 Then 'Shortest name is Brian
GridNames.Add(DataGridView1.CurrentCell.Value.ToString)
End If
Dim Difference As IEnumerable(Of String) = DropDownArray.Except(GridNames)
For x As Integer = 0 To DropDownArray.ToString.Length - 1
UpdatedNameList(x) = Difference(x)
Next
Dim combo As DataGridViewComboBoxCell
For row = 0 To DataGridView1.Rows.Count - 1
For col = 0 To DataGridView1.Columns.Count - 1
combo = DataGridView1(col, row)
If combo.Value.ToString.Length < 4 Then
combo.DataSource = UpdatedNameList
End If
Next
Next
End If
End Sub
我得到一個沒有參考我的代碼的中斷錯誤,所以我不知道我在搞砸什么。錯誤文本如下:
System.NullReferenceException:“物件參考未設定為物件的實體。”
Form1.vb 中的 NameGrid.Form1.DataGridView1_SelectionChanged(Object, System.Windows.Forms.DataGridViewCellEventArgs)
uj5u.com熱心網友回復:
很難提供一個好的答案,因為我覺得可能沒有一個好的答案。我不想勸阻您嘗試做的事情,但是我希望我可以讓您更好地了解實作您的要求所需的條件。基本上,這是我之前問題的評論(您忽略了)的延續……初始化 VB.NET DataGridView 以擁有所有組合框;從陣列填充的按鈕
對于初學者,代碼將組合框添加到網格中的單元格的方式是……。只是錯了。請讓我澄清一下……
有一個“型別”列,DataGridView專門用于“組合框”。它被稱為 a DataGridViewComboBoxColumn,它是你應該使用的。相反,您的代碼將DataGridViewTextBoxColumn's 添加到網格中,然后將 aDataGridViewComboBoxCell放入每個Text Box單元格中……?......這可能很有效,但它只會使事情復雜化并為您“創造”更多作業。有一個更好的辦法。
例如,查看 DataGridView 設定的第一段代碼……
DataGridView1.ColumnCount = 12
DataGridView1.RowCount = 12
DataGridView1.RowHeadersWidth = 50
For row As Integer = 0 To 11
DataGridView1.Rows(row).HeaderCell.Value = (row 1).ToString()
For col As Integer = 0 To 11
DataGridView1(col, row) = New DataGridViewComboBoxCell
Next
Next
你應該注意這段代碼的第一行……
DataGridView1.ColumnCount = 12
這是添加 12 個“文本框”列,您應該添加 12 個“組合框”列。如果您使用 aDataGridViewComboBoxColumn而不是 a DataGridViewTextBoxColumn,那么您可以洗掉回圈中添加DataGridViewComboBoxCell...
DataGridView1(col, row) = New DataGridViewComboBoxCell
網格將“自動”DataGridViewComboBoxCell為組合框列中的每個單元格創建一個。如果添加了新行,它將自動添加DataGridViewComboBoxCells. 因此,使用 a 來執行此操作DataGridViewComboBoxColumn可能看起來像……
For index = 1 To 12
DataGridView1.Columns.Add(New DataGridViewComboBoxColumn())
Next
DataGridView1.RowCount = 12
DataGridView1.RowHeadersWidth = 50
For row As Integer = 0 To 11
DataGridView1.Rows(row).HeaderCell.Value = (row 1).ToString()
Next
如果您運行上面的代碼,您會注意到每個單元格“已經”為 a DataGridViewComboBoxCell,您的代碼無需添加一個。此外,由于您的代碼使用的是 aTextBoxColumn這可能是代碼在線失敗的原因之一combo = DataGridView1(col, row)……。
接下來……關于具有不同值的組合框……您需要記住,您要管理 12x12=144 組合框!如果所有 144 個組合框專案串列都包含相同的值,那么它相當簡單,我們可以輕松地將每個組合框資料源設定為相同的專案串列。DataGridViewComboBoxColumn但是,將每個's設定為相同的串列會更容易,DataSource并且每列都會將此串列用于該列中的所有組合框單元格。因此,您可以更改上面的代碼,將組合框列添加到...
For index = 1 To 12
Dim col = New DataGridViewComboBoxColumn()
col.DataSource = ComboNames
DataGridView1.Columns.Add(col)
Next
這將消除對第二個代碼片段的需要 ComboBox Setup
但是,我的理解是您想要……如果用戶在一個組合框中“選擇”特定專案,那么,該選定專案將不會在任何其他組合框專案串列中可用。
這當然是可行的;但是,應該清楚的是……如果每個組合框選擇了不同的值……那么從技術上講……必須有 144 個不同的資料源!每個單獨的組合框資料源都必須是唯一的。
如果我們繼續……讓我們仔細看看當用戶更改組合框的選定值時所涉及的內容。最初,當表單加載時,所有組合框都是空的,每個組合框都使用“相同”的資料源。
現在用戶在其中一個“空”組合框中更改了選定的值……一旦用戶選擇了一個值,就會觸發一個網格事件,讓我們知道哪個單元格值發生了變化。假設用戶在組合框中選擇了“Choice 1”。由于所有組合框都使用相同的資料源并包含相同的值,因此我們可以簡單地從該專案串列中“洗掉選擇 1”。
HOWEVER, if we remove “Choice 1” item from the list… then the current combo box which HAS the value “Choice 1” selected will fail and you will get the grids DataError as “Choice 1” will not be in the combo boxes list of items. Therefore, we would need to add “Choice 1” ONLY to THAT combo boxes list of items. This makes THAT combo boxes data source UNIQUE and we can no longer use the same data source the other (non-selected) combo boxes use. Therefore let us assume that we create a unique list of items specifically for that combo box and continue.
Now the user selects a different combo box. We know that “Choice 1” will NOT be an item in the combo boxes list of items since we removed it from the list. Continuing, let us say the user selected “Choice 3” and the grids changed event fires and we proceeded as we did previously… we remove “Choice 3” from the list for all the combo boxes using the same data source and add “Choice 3” to the current combo boxes list of items. However…
We would ALSO need to remove “Choice 3” from ANY combo box that has already selected an item. We need to do this for each previously changed combo box since those combo boxes have their own UNIQUE list of items. Therefore, as the user changes more and more combo boxes, the code has to do more and more work.
Also note that the previous example “assumes” that when the user selects an item in the combo box, that the selected item in the combo box was originally “empty”… meaning nothing was previously selected. If the user selected a value for the combo box and sometime later “changed” that selected value to a different value… then… we would need to PUT BACK the previously selected value so the other combo boxes CAN select that value. This would include any previously changed combo box!
I hope you can see from the previous example, that it is NOT going to be trivial to manage 144 combo boxes in the manner you describe. Initially, when all the combo boxes are the same… it is trivial… however, as the user changes more and more combo boxes, it would not be surprising to possibly see a sluggish UI when combo box changes are made.
Please forgive my harangue and am only trying to help. It should be fairly clear that managing the combo boxes as you want will NOT be trivial especially using a DataGridViewComboBoxCell. I hope I cleared some things up.
And finally, I have to ask if you have seriously sat down and actually “used” your 12x12 grid of combo boxes… I ran a few tests and can only speak for myself… however… as a user… looking at 144 combo boxes was not pleasant. I am not sure what the overall goal is, however from a user perspective I would consider the 12x12 combo box grid a questionable UI choice. We already know it is NOT going to be trivial to implement and it is going to suck if you go to all the effort to make it work only to find the users hate it.
要點是……聽起來您可能需要回到“資料設計”階段。你想做的事情并沒有那么不尋常,但是組合框太多,每個組合框中的專案太多……這是“真正的”問題,是“資料設計”問題……不是 UI 問題。祝你好運。
uj5u.com熱心網友回復:
我解決了我的問題。它很慢而且效率很低,但這是另一個問題頁面。我最初遇到的問題是更改 DataGridView 中“未更改”單元格的資料源,同時保留已更改的單元格。容易做的事。但是我的代碼中的問題是對以下內容的呼叫:
If DataGridView1.CurrentCell.Value.ToString.Length > 4 Then
在未被觸及的單元格上沒有長度。如果您嘗試在“未更改”單元格上呼叫它,它將中斷但不會參考任何代碼。因此,我在每個單元格上回圈,CellValueChanged如果Try Catch失敗,我將 DataSource 更改為新陣列,如果沒有,我只需將值添加到網格中正在運行的條目串列中。它很慢(單元格更改和允許我更改下一個單元格之間大約 4 秒)但可以。我愿意進行優化,但同樣,答案是“不要嘗試獲取未更改單元格的長度”。
更新未更改單元格以具有更新名稱串列()的新組合框資料源的作業代碼:
Private Sub DataGridView1_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Dim UpdatedNameList As List(Of String) = DropDownArray
Dim combo2 As DataGridViewComboBoxCell
If started = 1 Then
For row = 0 To DataGridView1.Rows.Count - 1
For col = 0 To DataGridView1.Columns.Count - 1
Try
If DataGridView1(col, row).Value.ToString.Length >= 4 Then
GridNames.Add(DataGridView1(col, row).Value.ToString)
End If
Catch
Dim Difference As IEnumerable(Of String) = DropDownArray.Except(GridNames)
For x As Integer = 0 To DropDownArray.ToString.Length - 1
UpdatedNameList(x) = Difference(x)
Next
combo2 = DataGridView1(col, row)
combo2.DataSource = UpdatedNameList
End Try
Next
Next
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/415367.html
標籤:
