CSS Style Sheet

Posted on 2004-11-10
Last Modified: 2010-04-24
I have web app. VB.Net. Four controls on aspx page. Sqlconnection, Dataadapter, Dataset, and Datagrid. The data on the grid is from sql2000 and each record has a date assigned to it. Each record represents appointment made by customer. I need the record s from the past to show up as the color red.

Example: If appointment is made for today 11/10/2004
              Then on 11/11/2004, on the grid, turn that record red.

Question by:Donnie4572
    LVL 28

    Accepted Solution

    here is an example that might help you

    (delete windows generated code, add starting with Sub new)

    Option Explicit On

    Imports Microsoft.VisualBasic
    Imports System
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Data.Common
    Imports System.Data.OleDb
    Imports System.Drawing
    Imports System.Windows.Forms

    Public Class Form1
        Inherits Form
        Private WithEvents dataGrid1 As DataGrid
        Private components As Container
     Public Sub New()
            ' Required for Windows Form Designer support

        End Sub
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

            If disposing Then
                If (Not (components) Is Nothing) Then
                End If
            End If

        End Sub
        Private Sub InitializeComponent()

            Me.dataGrid1 = New System.Windows.Forms.DataGrid()
            CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
            ' dataGrid1
            Me.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right)
            Me.dataGrid1.DataMember = ""
            Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
            Me.dataGrid1.Location = New System.Drawing.Point(24, 24)
            Me.dataGrid1.Name = "dataGrid1"
            Me.dataGrid1.Size = New System.Drawing.Size(448, 280)
            Me.dataGrid1.TabIndex = 0
            ' Form1
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(504, 341)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dataGrid1})
            Me.Name = "Form1"
            Me.Text = "Form1"
            AddHandler Me.Load, New System.EventHandler(AddressOf Form1_Load)
            CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()

        End Sub
        <STAThread()> _
        Public Shared Sub Main()

            Application.Run(New Form1())

        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

            ' Set the connection and sql strings
            ' assumes your mdb file is in your root
            Dim connString As String
            connString = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\testdb\Northwind.mdb"
            Dim sqlString As String
            sqlString = "SELECT * FROM customers"
            Dim dataAdapter As OleDbDataAdapter
            dataAdapter = Nothing
            Dim _dataSet As DataSet
            _dataSet = Nothing
            ' Create a table style that will hold the new column style
            ' that we set and also tie it to our customer's table from our DB
                ' Connection object
                Dim connection As OleDbConnection
                connection = New OleDbConnection(connString)
                ' Create data adapter object
                dataAdapter = New OleDbDataAdapter(sqlString, connection)
                ' Create a dataset object and fill with data using data adapter's Fill method
                _dataSet = New DataSet()
                dataAdapter.Fill(_dataSet, "customers")
            Catch ex As Exception
                MessageBox.Show(((((("Problem with DB access-" & Microsoft.VisualBasic.ChrW(10) & Microsoft.VisualBasic.ChrW(10) & "   connection: " + connString) _
                                + "" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "            query: ") _
                                + sqlString) _
                                + "" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10)) _
                                + ex.ToString))
            End Try
            Dim tableStyle As DataGridTableStyle
            tableStyle = New DataGridTableStyle()
            tableStyle.MappingName = "customers"
            ' since the dataset has things like field name and number of columns,
            ' we will use those to create new columnstyles for the columns in our DB table
            Dim numCols As Integer
            numCols = _dataSet.Tables("customers").Columns.Count
            Dim aColumnTextColumn As DataGridColoredTextBoxColumn
            Dim i As Integer
            i = 0

            Do While (i < numCols)
                aColumnTextColumn = New DataGridColoredTextBoxColumn()
                aColumnTextColumn.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
                aColumnTextColumn.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName
                i = (i + 1)

            ' make the dataGrid use our new tablestyle and bind it to our table
            dataGrid1.DataSource = _dataSet.Tables("customers")

        End Sub
    End Class

    Option Explicit On

    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Windows.Forms

    Public Class DataGridColoredTextBoxColumn
        Inherits DataGridTextBoxColumn
        Public Sub New()
            'Warning: Implementation not found
        End Sub
        Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

            ' the idea is to conditionally set the foreBrush and/or backbrush
            ' depending upon some crireria on the cell value
            ' Here, we color anything that begins with a letter higher than 'F'
                Dim o As Object
                o = Me.GetColumnValueAtRow(source, rowNum)
                If (Not (o) Is Nothing) Then
                    Dim c As Char
                    c = CType(o, String).Substring(0, 1)
                    If (c > "F") Then
                         backBrush = new SolidBrush(Color.Pink)
                        foreBrush = New SolidBrush(Color.White)
                    End If
                End If
            Catch ex As Exception
                ' empty catch
                ' make sure the base class gets called to do the drawing with
                ' the possibly changed brushes
                MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
            End Try

        End Sub
    End Class

    LVL 12

    Author Comment

    iboutchkine, thanks for reply.

    Ok, this code  works only I can't get it to work with datetime column. Can someone show me how to use this code for my datetime field?

    <asp:datagrid id="datagrid1" runat="server" OnItemDataBound="myformatrow" AutoGenerateColumns="False" Height="99.7%" Width="99.7%" AllowSorting="True">

     Protected Sub myformatrow(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim mycol As String = Convert.ToString(e.Item.Cells(2).Text)
            If mycol = "mydata" Then
            e.Item.ForeColor = Color.Red
          End If
        End If
    End Sub

    LVL 12

    Author Comment

    Well it turns out this does work.
    If mycol = "10/15/2004 9:00:00 AM" Then

    The problem was 10/15/2004 09:00:00 AM  I suppose the 09 needed to 9 since it was converted to string.

    Will award points for effort

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now