Thanks to RonaldBiemans I have a DataGrid bound and formatted to the hard drive information from a series of servers (the names of which I am returning via a shared function / datareader from a SQL table) - What I need to do is to try and throw some conditional formatting into the mix. I need to evaluate the free space number and determine the percentage against the drive size and highlight the cell (free space) accordingly, per the below:
> 25% free = white
<= 25% free = yellow
<= 10% free = orange
<= 5% free = red
Below is the code - Thanks!
'-------------------------
----------
----------
----------
----------
----------
------
Imports System.Management
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Collections.SortedL
ist
Imports Microsoft.VisualBasic.Coll
ection
Imports System.Drawing
Imports System.Web.Mail
Partial Class sandbox2_aspx
Dim dt As New DataTable
Dim dt2 As New DataTable
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dr As SqlDataReader = Data.ServersGET()
dt.Columns.Add("Server", GetType(System.String))
dt.Columns.Add("Diskname",
GetType(System.String))
dt.Columns.Add("Disk Size")
dt.Columns.Add("Space Used")
dt.Columns.Add("Free Space")
While dr.Read()
Dim strComputer As String = dr("SERVER").ToString()
SystemList(strComputer)
End While
With datagrid1
.BorderColor = System.Drawing.Color.Gray
.CellPadding = 3
.AutoGenerateColumns = True
.HeaderStyle.Font.Name = "verdana"
.HeaderStyle.Font.Size = FontUnit.Parse("10px")
.ItemStyle.Font.Name = "verdana"
.ItemStyle.Font.Size = FontUnit.Parse("10px")
.HeaderStyle.Font.Bold = True
.HeaderStyle.BackColor = System.Drawing.Color.FromA
rgb(37, 51, 82)
.HeaderStyle.ForeColor = System.Drawing.Color.White
.DataSource = Diskdata()
.DataBind()
.Visible = True
.ShowFooter = False
End With
End Sub
Private Sub SystemList(ByVal strComputer As String)
Dim co As ConnectionOptions = New ConnectionOptions
co.Username = "nsadm13"
co.Password = "houston"
Dim ms As System.Management.Manageme
ntScope = New System.Management.Manageme
ntScope("\
\" & strComputer & "\root\cimv2", co)
Dim oq As System.Management.ObjectQu
ery = New System.Management.ObjectQu
ery("Selec
t * from Win32_LogicalDisk where DriveType=3")
Dim Query1 As ManagementObjectSearcher = New ManagementObjectSearcher(m
s, oq)
Dim queryCollection1 As ManagementObjectCollection
= Query1.Get()
Dim mo As ManagementObject
For Each mo In queryCollection1
Dim dr As DataRow = dt.NewRow
Dim SU As Double = CType(mo("Size").ToString,
Long) - CType(mo("FreeSpace").ToSt
ring, Long)
dr.Item("Server") = strComputer.ToUpper()
dr.Item("Diskname") = mo("Name").ToString().ToUp
per()
dr.Item("Disk Size") = Data.ConvertBytes(CType(mo
("Size").T
oString, Long))
dr.Item("Space Used") = Data.ConvertBytes(CType(SU
, Long))
dr.Item("Free Space") = Data.ConvertBytes(CType(mo
("FreeSpac
e").ToStri
ng, Long))
dt.Rows.Add(dr)
Next
End Sub
Private Function Diskdata() As DataTable
Dim dt2 As New DataTable
dt2.Columns.Add("Server")
dt2.Columns.Add("DiskInfo"
)
For Each dr2 As DataRow In dt.Rows
If Not dt2.Columns.Contains(dr2.I
tem("Diskn
ame").ToSt
ring) Then
dt2.Columns.Add(dr2.Item("
Diskname")
.ToString,
GetType(System.String))
End If
Next
Dim dv2 As DataView = dt2.DefaultView
Dim ex As Integer
For x As Integer = 0 To dt.Rows.Count - 1
Dim dr2 As DataRow
Dim dr7 As DataRow
Dim dr8 As DataRow
dv2.Sort = "Server"
ex = dv2.Find(dt.Rows(x).Item(0
))
If ex >= 0 Then
dr2 = dt2.Rows(ex)
dr7 = dt2.Rows(ex + 1)
dr8 = dt2.Rows(ex + 2)
Else
dr2 = dt2.NewRow
dr7 = dt2.NewRow
dr8 = dt2.NewRow
End If
dr2(0) = dt.Rows(x).Item(0)
dr7(0) = dt.Rows(x).Item(0)
dr8(0) = dt.Rows(x).Item(0)
dr2(1) = dt.Columns(2).ColumnName
dr7(1) = dt.Columns(3).ColumnName
dr8(1) = dt.Columns(4).ColumnName
dr2.Item(dt2.Columns.Index
Of(dt.Rows
(x).Item("
diskname")
)) = dt.Rows(x).Item(2)
dr7.Item(dt2.Columns.Index
Of(dt.Rows
(x).Item("
diskname")
)) = dt.Rows(x).Item(3)
dr8.Item(dt2.Columns.Index
Of(dt.Rows
(x).Item("
diskname")
)) = dt.Rows(x).Item(4)
If ex = -1 Then
dt2.Rows.Add(dr2)
dt2.Rows.Add(dr7)
dt2.Rows.Add(dr8)
End If
Next
dv2.Sort = ""
dv2 = Nothing
For x As Integer = 0 To dt2.Rows.Count
If x Mod 3 <> 0 Then
dt2.Rows(x).Item(0) = ""
End If
Next
Return (dt2)
End Function
Private Sub Grid1_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
em Then
e.Item.Cells(0).Font.Bold = True
e.Item.Cells(0).Horizontal
Align = HorizontalAlign.Left
e.Item.Cells(1).Horizontal
Align = HorizontalAlign.Left
'e.Item.HorizontalAlign = HorizontalAlign.Center
ElseIf e.Item.ItemType = ListItemType.Header Then
'e.Item.Cells(1).Horizonta
lAlign = HorizontalAlign.Right
End If
End Sub
End Class
Start Free Trial