Combobox Items aus DataGridView

  • VB.NET
  • .NET (FX) 4.0

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Combobox Items aus DataGridView

    Hallo,

    mit dem folgenden Befehl will ich die Daten der Spalte 1 aus dem DGV der Combobox hinzufügen

    VB.NET-Quellcode

    1. sql2 = "SELECT * FROM Trainingsplan"
    2. abfrqage_ausfuehren_start_form_trainingsplan(sql2)
    3. For i = 0 To Me.DataGridView1.Rows.Count - 1
    4. Me.ComboBox1.Items.Add(Me.DataGridView1.Item(1, i).Value)
    5. Next


    Es wird folgender Fehler angezeigt:
    System.ArgumentNullException wurde nicht behandelt.
    HResult=-2147467261
    Message=Der Wert darf nicht NULL sein.
    Parametername: item
    ParamName=item
    Source=System.Windows.Forms
    StackTrace:
    bei System.Windows.Forms.ComboBox.ObjectCollection.AddInternal(Object item)
    bei System.Windows.Forms.ComboBox.ObjectCollection.Add(Object item)
    bei RoadToSuccess.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\Niklas\Documents\Desktop\RoadToSuccess\RoadToSuccess\Form1.vb:Zeile 49.
    bei System.EventHandler.Invoke(Object sender, EventArgs e)
    bei System.Windows.Forms.Form.OnLoad(EventArgs e)
    bei System.Windows.Forms.Form.OnCreateControl()
    bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    bei System.Windows.Forms.Control.CreateControl()
    bei System.Windows.Forms.Control.WmShowWindow(Message& m)
    bei System.Windows.Forms.Control.WndProc(Message& m)
    bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    bei System.Windows.Forms.ContainerControl.WndProc(Message& m)
    bei System.Windows.Forms.Form.WmShowWindow(Message& m)
    bei System.Windows.Forms.Form.WndProc(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    bei System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
    bei System.Windows.Forms.Control.SetVisibleCore(Boolean value)
    bei System.Windows.Forms.Form.SetVisibleCore(Boolean value)
    bei System.Windows.Forms.Control.set_Visible(Boolean value)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.Run(ApplicationContext context)
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    bei RoadToSuccess.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
    bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    bei System.Threading.ThreadHelper.ThreadStart()
    InnerException:

    Wie kann ich dieses Problem lösen?

    Leo&Eike schrieb:

    System.ArgumentNullException wurde nicht behandelt.

    Leo&Eike schrieb:

    VB.NET-Quellcode

    1. Me.ComboBox1.Items.Add(Me.DataGridView1.Item(1, i).Value)
    Ich nehme mal an, dass das Item(1, i) leer ist, also über kein Value verfügt.
    Also teste das einfach ab:

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2. For i = 0 To Me.DataGridView1.Rows.Count - 1
    3. If Me.DataGridView1.Item(1, i).Value IsNot Nothing Then
    4. Me.ComboBox1.Items.Add(Me.DataGridView1.Item(1, i).Value)
    5. End If
    6. Next
    7. End Sub
    Bilder
    • ArgumentNullException.jpg

      31,92 kB, 537×264, 55 mal angesehen
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!