Solved

Color datagrid rows using .net 3.5 COMPACT

Posted on 2010-11-30
2
5,938 Views
Last Modified: 2013-12-27
Hi there,

I am trying to set the colours of a datagrid based on a value.
Trouble is the .net 3.5 COMPACT framework is different to what I am used to.

Normally to colour the rows I would use something like .......

Dim r As Integer
            For r = 0 To MYDATAGRID.Rows.Count - 1
                If Not IsDBNull(MYDATAGRID.Rows(r).Cells(11).Value) Then

                    If MYDATAGRID.Rows(r).Cells(11).Value = "True" Then
                        MYDATAGRID.Rows(r).DefaultCellStyle.BackColor = Color.Yellow
                    Else
                        MYDATAGRID.Rows(r).DefaultCellStyle.BackColor = Color.White
                    End If
                End If
           Next

Trouble is with the .net 3.5 Compact framework, I get the error
'Rows' is not a member of 'System.Windows.Forms.DataGrid'.

Does any one have an example of how to colour a datagrid row using the compact framework ?

The project is being delployed on a Windows Mobile 6 Smart Device, if that helps ?

This is the code I am using  to populate the grid

    Protected Sub Populate_Customer_List()

        ' Display the Loaded Customer List
        '''''''''''''''''''''''''''''''''''
        Dim MyConn As New SqlCeConnection(MyLocalConnString)
        Dim MyCmd As SqlCeCommand = MyConn.CreateCommand()
        Dim MyDataSet As New Data.DataSet()
        Dim MyDataAdapter As New SqlCeDataAdapter(MyCmd)

        Dim MySQl As String

        MySQl = "select max(deliveryorder) As DeliveryOrder, "
        MySQl = MySQl & "custnmbr, "
        MySQl = MySQl & "max(shiptoname), "
        MySQl = MySQl & "max(zipcode), "
        MySQl = MySQl & "max(received), "
        MySQl = MySQl & "max(cast(completed as nvarchar))"
        MySQl = MySQl & "from bigk_myt_pda_out_lines  "
        MySQl = MySQl & "where custnmbr <> 'overs' "
        MySQl = MySQl & "group by custnmbr "
        MySQl = MySQl & "order by DeliveryOrder "

        MyCmd.CommandText = MySQl
        MyCmd.CommandType = Data.CommandType.Text

        MyConn.Open()
        MyDataAdapter.Fill(MyDataSet)
        DG_Customer_List.DataSource = MyDataSet.Tables(0)
        MyRowCount = MyDataSet.Tables(0).Rows.Count
        MyConn.Close()

    End Sub

Thanks in advance

Paul

0
Comment
Question by:PaulEll
2 Comments
 
LVL 16

Accepted Solution

by:
hjgode earned 500 total points
Comment Utility
Hello

you have to overwrite the cells paint method as CF3.5 does not support what you try.

See http://stackoverflow.com/questions/1883863/conditional-formatting-in-a-datagrid-using-net-compact-framework-3-5

In VB this should be:

Public Class DataGridExtendedTextBoxColumn
      Inherits DataGridTextBoxColumn
      ' I use the Form to store Brushes and the Font, feel free to handle it differently.
      Private parent As Form1

      Public Sub New(parent As Form1)
            Me.parent = parent
      End Sub

      ' You'll need to override the paint method
      ' The easy way: only change fore-/backBrush
      Protected Overrides Sub Paint(g As Graphics, bounds As Rectangle, source As CurrencyManager, rowNum As Integer, backBrush As Brush, foreBrush As Brush, _
            alignToRight As Boolean)
            MyBase.Paint(g, bounds, source, rowNum, parent.redBrush, parent.fontBrush, _
                  alignToRight)
      End Sub
End Class

The hard way: draw it yourself

Protected Overrides Sub Paint(g As Graphics, bounds As Rectangle, source As CurrencyManager, rowNum As Integer, backBrush As Brush, foreBrush As Brush, _
      alignToRight As Boolean)
      ' Background
      g.FillRectangle(parent.redBrush, bounds)

      ' Get and format the String
      Dim sf As New StringFormat()
      Dim dataRowView As DataRowView = DirectCast(source.List(rowNum), DataRowView)
      Dim row As DataRow = dataRowView.Row
      Dim value As [Object] = row(Me.MappingName)
      Dim str As [String]
      If value.Equals(System.DBNull.Value) Then
            str = Me.NullText
      ElseIf Me.Format.Length <> 0 Then
            ' NOTE: Formatting is handled differently!
            str = [String].Format(Me.Format, value)
      Else
            str = value.ToString()
      End If

      ' Draw the String
      g.DrawString(str, parent.font, parent.fontBrush, New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height))

      'base.Paint(g, bounds, source, rowNum, parent.redBrush, parent.fontBrush, alignToRight);
End Sub


' The "this" is due to the new constructor
Me.dataGridTextBoxColumn1 = New DataGridExtendedTextBoxColumn(Me)
Me.dataGridTableStyle1.GridColumnStyles.Add(Me.dataGridTextBoxColumn1)
0
 
LVL 1

Author Closing Comment

by:PaulEll
Comment Utility
C
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now