Solved

Color datagrid rows using .net 3.5 COMPACT

Posted on 2010-11-30
2
6,373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
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 video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

630 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