Click here to Skip to main content
15,896,153 members

Compare cell value with header and change cell color DataGrid

LordVovin asked:

Open original thread
Hi All,

I don't know if it's possible, but I would like to check if the cell value corresponds to Header value, if yes - color cell. I have created a calendar in datagrid which, after selecting the dates from DateTimePickers creates a column with the date corresponding to the selected date and imports data from a SQL table.

http://oi59.tinypic.com/kcjvix.jpg[^]

This is my app. The code:

VB
        Dim sql As String
        DataGridView1.DataContext = Nothing
        DataGridView1.Columns.Clear()
        DataGridView1.Visibility = Windows.Visibility.Visible
        Dim sDate As DateTime = Format(DateTimePicker1.SelectedDate, "MM/dd/yyyy")
        Dim eDate As DateTime = Format(DateTimePicker2.SelectedDate, "MM/dd/yyyy")
        sql = "SELECT Enumber, StartDate, EndDate from tbl_Test where StartDate = '" & sDate & "' AND EndDate = '" & eDate & "' "
        Dim da As New SqlDataAdapter(sql, cnn)
        Try
            Dim dt As New DataTable("Accs")
            da.Fill(dt)
            DataGridView1.ItemsSource = dt.DefaultView
            Dim startDate As DateTime = DateTimePicker1.SelectedDate
            Dim endDate As DateTime = DateTimePicker2.SelectedDate
            Dim numberOfDays As Integer = ((endDate.Date) - (startDate.Date)).Days + 1
            For dayCount As Integer = -1 To (numberOfDays - 0)
                Dim currentDate As Date = Convert.ToDateTime(startDate.AddDays(dayCount))
                Dim formatedD As String = currentDate.ToString("dd")
                If currentDate.DayOfWeek <> DayOfWeek.Saturday AndAlso currentDate.DayOfWeek <> DayOfWeek.Sunday Then
                    Dim c1 As New DataGridTextColumn
                    c1.Header = formatedD
                    DataGridView1.Columns.Add(c1)
End If
            Next dayCount
        dt = Nothing
            da.Dispose()
            cnn = Nothing
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
        End Try

Scenario:

An employee selects the dates from DateTimePickers
On button click DataGrid will appear with automatically generated columns.(of the days)
If the columns StartDate and EndDate (columns taken from the table) are the dates - the cells corresponding to those dates change to blue.

Thank you very much for your help!

BR
Tags: Visual Basic, WPF, DataGrid

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900