Advertisement

11.18.2004 at 12:32PM PST, ID: 21212269
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Datagrid - Conditional formatting

Asked by tbaseflug in .NET

Tags: , ,

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.SortedList
Imports Microsoft.VisualBasic.Collection
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.FromArgb(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.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 DataRow = dt.NewRow
            Dim SU As Double = CType(mo("Size").ToString, Long) - CType(mo("FreeSpace").ToString, Long)
            dr.Item("Server") = strComputer.ToUpper()
            dr.Item("Diskname") = mo("Name").ToString().ToUpper()
            dr.Item("Disk Size") = Data.ConvertBytes(CType(mo("Size").ToString, Long))
            dr.Item("Space Used") = Data.ConvertBytes(CType(SU, Long))
            dr.Item("Free Space") = Data.ConvertBytes(CType(mo("FreeSpace").ToString, 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.Item("Diskname").ToString) 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.IndexOf(dt.Rows(x).Item("diskname"))) = dt.Rows(x).Item(2)
            dr7.Item(dt2.Columns.IndexOf(dt.Rows(x).Item("diskname"))) = dt.Rows(x).Item(3)
            dr8.Item(dt2.Columns.IndexOf(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.AlternatingItem Then
            e.Item.Cells(0).Font.Bold = True
            e.Item.Cells(0).HorizontalAlign = HorizontalAlign.Left
            e.Item.Cells(1).HorizontalAlign = HorizontalAlign.Left
            'e.Item.HorizontalAlign = HorizontalAlign.Center
        ElseIf e.Item.ItemType = ListItemType.Header Then
            'e.Item.Cells(1).HorizontalAlign = HorizontalAlign.Right
        End If

    End Sub

End ClassStart Free Trial
[+][-]11.18.2004 at 02:45PM PST, ID: 12620035

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.18.2004 at 09:19PM PST, ID: 12622108

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.19.2004 at 06:18AM PST, ID: 12625382

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.19.2004 at 06:42AM PST, ID: 12625618

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.19.2004 at 08:07AM PST, ID: 12626609

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.22.2004 at 01:01AM PST, ID: 12642562

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.22.2004 at 02:38AM PST, ID: 12642988

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: .NET
Tags: conditional, datagrid, formatting
Sign Up Now!
Solution Provided By: RonaldBiemans
Participating Experts: 3
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32