主頁 > 軟體設計 > 在VB.NET中,有沒有辦法將未更改單元格的DataGridViewComboBox下拉串列更改為新的串列陣列,同時保留更改的單元格?

在VB.NET中,有沒有辦法將未更改單元格的DataGridViewComboBox下拉串列更改為新的串列陣列,同時保留更改的單元格?

2022-01-19 18:33:52 軟體設計

我創建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

標籤:

上一篇:將ms訪問accdb資料庫的表名存盤到vb.net中的陣列中

下一篇:基于組合框選定項填充datagridview,該組合框取決于資料庫中另一個選定的組合框

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more