Solved

Color datagrid rows using .net 3.5 COMPACT

Posted on 2010-11-30
2
6,272 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Filesystem watcher not working 5 58
VB6 calling COM DLL written in Visual Studio 2003 6 58
Jail break Samsung Galaxy S8 phone 3 87
Beacon technology 3 30
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Suggested Courses

752 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