Link to home
Start Free TrialLog in
Avatar of tbaseflug
tbaseflugFlag for United States of America

asked on

Hard Drive Space Values into Datagrid as columns

I am looking to get the hard drives and spaces from my servers and have the below working great to return it via an array - but wanted to do return each  value as a column in a datagrid - is this possible and if so how?
--------------------
    Private Function SystemList(ByVal strComputer As String) As ArrayList

        Dim al2 As New ArrayList
        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = "Nsadm13"
        co.Password = "houston"
        Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
        Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("Select * from Win32_LogicalDisk where DriveType=3")
        Dim Query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
        Dim queryCollection1 As ManagementObjectCollection = Query1.Get()
        Dim mo As ManagementObject

        For Each mo In queryCollection1
            al2.Add( _
             "<b>Drive</b>: " & mo("Name").ToString() & _
                        "<br><b>Free Space</b>: " & Data.ConvertBytes(CType(mo("FreeSpace").ToString, Long)) & _
                        "<br><b>Size</b>: " & Data.ConvertBytes(CType(mo("Size").ToString, Long)) _
                       )
        Next
        Return (al2)

    End Function
Avatar of RonaldBiemans
RonaldBiemans

Instead of using an arraylist use a datatable
Avatar of tbaseflug

ASKER

Any way that you could show me based upon my code above?
Private Function SystemList(ByVal strComputer As String) As datatable

        Dim al2 As New datatable
        al2.columns.add("Diskname", gettype(System.string))
        al2.columns.add("FreeSpace", gettype(System.int32))
        al2.columns.add("Size", gettype(System.int32))

        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = "Nsadm13"
        co.Password = "houston"
        Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
        Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("Select * from Win32_LogicalDisk where DriveType=3")
        Dim Query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
        Dim queryCollection1 As ManagementObjectCollection = Query1.Get()
        Dim mo As ManagementObject

        For Each mo In queryCollection1
dim dr as datarows = dt.newrow
dr.item("Diskname") = mo("Name").ToString()
dr.item("FreeSpace") = Data.ConvertBytes(CType(mo("FreeSpace").ToString, int32))
dr.item("Size") = Data.ConvertBytes(CType(mo("Size").ToString, int32))
dt.rows.add(dr)
        Next
        Return (al2)

    End Function
RonaldBiemans -

That works great!!!  However, is there any way to flip it so that the drives appear across the top as column headers and that the size and space columns are rows under then., e.g.

C     D     E
---   ---   ---
10   15   20
Hmmm, there is but you have to do a bit of cheating, The trick is to transpose your datatable.

Can this wait till tomorrow because I have to leave now, but I can give you the code first thing in the morning



Certainly - I just appreciate the help!
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RonaldBiemans -

Wow, that works great!!! You have certainly earned the points.  I am going to open another 500 pt question as I have a list of server names coming from my SQL db and am looping through the server names in a datareader and will need to add the server name to the grid as well - you have earned the pts for this one and will certyainly give another 500 if you want to help on the last part of this?