Solved

Color datagrid rows using .net 3.5 COMPACT

Posted on 2010-11-30
2
6,214 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
ID: 34245341
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
ID: 34838955
C
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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