DataGridViewRowCollection troubles

I have a DataGridViewRowCollection() and it's size is dynamic. I tried to add a row collection to each index of my DataGridViewRowCollection() like so:

Dim DgvRows as DataGridViewRowCollection()

ReDim DgvRows(6)
DgvRows(0).add(DgvMain.Rows)

I have also tried

For Int as Integer = 0 to DgvMain.Rows - 1
DgvRows(0).Add(DgvMain.Rows(Int)
Next

Each time I get a NullReferenceException. Details follow:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="RAAP"
StackTrace:
at RAAP.Form1.StartSearch() in C:\Documents and Settings\Troy Lundin\My Documents\Visual Studio 2005\Projects\RAAP\RAAP\Form1.vb:line 103
at RAAP.Form1.MiSearch_Click(Object sender, EventArgs e) in C:\Documents and Settings\Troy Lundin\My Documents\Visual Studio 2005\Projects\RAAP\RAAP\Form1.vb:line 39
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at RAAP.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Any Ideas on what I am doing wrong

Thanks,
Troy Lundin




Answer this question

DataGridViewRowCollection troubles

  • Greenstrike

    There's nothing to stop you doing something like the following, creating a class to store the three bits of info and then using a standard collection such as a arraylist.   Much simple and has nothing do do with datagridview.   Datagridviewrows is a collection type associated with a datagridview control - if your not using the control then I wouldnt be messing around with that collection.

     

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As New ArrayList
            x.Add(New Info("a", "b", "c"))
            x.Add(New Info("a2", "b2", "c2"))
            x.Add(New Info("a3", "b3", "c3"))
        End Sub
    End Class


    Class Info
        Sub New(ByVal x As String, ByVal y As String, ByVal z As String)
            a = x
            b = y
            c = y
        End Sub

        Public a As String = ""
        Public b As String = ""
        Public c As String = ""
    End Class


     

     


  • KentaroM

    Your last sentence was mildly correct. I want to store DataGridViewRows though because each row contains three pieces of data. All pieces are strings so I could use a string array, but I thought it would be easier to store the rows as a whole.

  • djchapin

    Why are you using a datagridviewrowscollection as in your description you actually make no reference to a datagridview control at all.

    You could use a normally collection controls such as ArrayList, List , Array, Hashtable to add the drives that you have found.

    It sounds as though you want to search all the drives for a specific file and for each drive you find the file, store the path to that file in a collection.


  • williamguy

    Ok, that didn't work. Let me tell you what I am trying to do.

    DgvRows.Length needs to equal the amount of drives on the computer. Thus it must change. My program searches every drive of a computer and looks for a certain file. If that file is found it's location is added to DgvMain. When My program has finished searching one drive I want it to store the rows of DgvMain in DgvRows(Drive). Drive is the zero based index of which drive was scanned (C:\ = 0, F:\ = 3). Then once all drives have been scanned, I want to be able to show only the locations on whichever drives the user want to see.

    So say my computer has four drives (C:\, D:\, E:\, F:\). I would ReDim DgvRows to be size four. Now, say each drive has the searched for file on it six times. Then DgvRows(0).Count should equal six and should contain all six rows from DgvMain (6 files found = 6 rows).

    I hope that wasn't too confusing.

    Thanks,
    Troy Lundin



  • elhussein

    The problem is normally because you havent arent using an instantiate object.

    Either use a NEW keyword in which case you need to specify the datagridview control in the constructor

    Dim DgvRows As New DataGridViewRowCollection(DataGridView1)

    Or if this is a reference variable then point it to a specific datagridview rows collection.

    Dim DgvRows1 As DataGridViewRowCollection

    DgvRows1 = DataGridView1.Rows

    Once this is done then its a collection so you can just use the collection methods such as add etc.


  • DataGridViewRowCollection troubles