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
Solved

Color datagrid rows using .net 3.5 COMPACT

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help converting a bitmap to an image in VB.Net 1 42
Help with exporting to excel 4 35
Jailbreak availability for new iPhone 7 Plus 2 35
vb.net class 3 21
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…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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