|
-
Jul 6th, 2023, 10:01 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] DGV formatting
I have a planner with a dgv listing a set of tasks. Everything is a task, but some tasks are designated as meetings based on a bit column named colMeeting. If this column is true, then the task is a meeting/appointment. The bottom 4 tasks meet that criteria and are displayed at the bottom of the list. There is also a query that would display only meeting tasks.
The DGV has general settings that set the forecolor for all of the displayed rows to Black.

Code:
#Region "Color Settings"
BackColor = Color.SteelBlue
tlsList.BackColor = Color.LightSteelBlue
tlsList.ForeColor = Color.Black
dgvList.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black
dgvList.RowsDefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.BackgroundColor = Color.SteelBlue
dgvList.DefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.DefaultCellStyle.ForeColor = Color.Black
#End Region
What I would like to do is make the meetings/appointments display to be a different forecolor.
I have attempted a couple of ideas, and failed. I really have no idea at all how one could approach this or even if it is possible.
So my question is can one have specific rows in a DGV with different format settings than other rows in a DGV and how would one go about doing that?
-
Jul 6th, 2023, 10:18 AM
#2
-
Jul 6th, 2023, 10:25 AM
#3
Thread Starter
Fanatic Member
Re: DGV formatting
Thanks.
I am currently looking at your 3rd suggestion (the title fits). Right now my head is spinning looking at the code, but I might be able to figure it out in a day or two. I will let you know how it goes, as soon as it goes.
-
Jul 6th, 2023, 11:24 AM
#4
Re: DGV formatting
I must be missing something.... A form with a DGV and a button
Code:
Public Class Form1
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DataGridView1.DataSource = GetTable()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DataGridView1.DataSource = GetTable()
For Each rw As DataGridViewRow In DataGridView1.Rows
If rw.IsNewRow Then Exit Sub
If DirectCast(rw.Cells(0).Value, Integer) = 100 Then
If DirectCast(rw.Cells(2).Value, String) = "11" Then
rw.DefaultCellStyle.ForeColor = Color.Red
rw.DefaultCellStyle.BackColor = Color.LightBlue
rw.Cells(1).Style.BackColor = Color.Yellow
End If
End If
Next
End Sub
Private Function GetTable() As DataTable
Dim table As New DataTable
table.Columns.Add("Col1", GetType(Integer))
table.Columns.Add("Col2", GetType(String))
table.Columns.Add("Col3", GetType(String))
' Add five rows with those columns filled in the DataTable.
table.Rows.Add(25, "Col1 A", "10")
table.Rows.Add(50, "Col1 B", "50")
table.Rows.Add(10, "Col1 C", "51")
table.Rows.Add(21, "Col1 D", "52")
table.Rows.Add(100, "Col1 E", "11")
table.Rows.Add(100, "Col1 F", "12")
table.Rows.Add(100, "Col1 G", "11")
table.Rows.Add(100, "Col1 H", "12")
Return table
End Function
End Class
-
Jul 6th, 2023, 12:22 PM
#5
Thread Starter
Fanatic Member
Re: DGV formatting
I must be missing someing...
I don't believe you are. I do have a button that is used to select which sort query I want to run. Additionally, one of the "DueDate" query is automatically ran when the form is opened.
I have been displaying my DGV formats as a single block of code (see below) that instead of looping, applies the formats to the entire DGV. I think that it is going to take a looping method of applying the formats row by row.
What you see below is the only way I have ever displayed a DGV and I do not believe that can be made to work with what I want to do. At any rate, I am looking at replacing what I have with a loop that will display the rows and their individual formats. That is what I am trying to figure out how to do at this moment. I think that what you are suggesting would fit a loop approach, instead of applying the formats to the entire DGV.
Am I understanding things correctly?
Code:
Private Sub SetState()
#Region "Color Settings"
BackColor = Color.SteelBlue
tlsList.BackColor = Color.LightSteelBlue
tlsList.ForeColor = Color.Black
dgvList.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black
dgvList.RowsDefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.BackgroundColor = Color.SteelBlue
dgvList.DefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.DefaultCellStyle.ForeColor = Color.Black
#End Region
#Region "Default settings"
Text = ("Task List").ToString
#End Region
#Region "DGV Settings"
dgvList.Enabled = True
dgvList.EnableHeadersVisualStyles = False
dgvList.AllowUserToAddRows = False
dgvList.AllowUserToDeleteRows = False
dgvList.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.ColumnHeadersVisible = True
dgvList.RowHeadersVisible = False
dgvList.Columns(0).Visible = True
dgvList.Columns(0).Width = 200
dgvList.Columns(0).HeaderText = "Task"
dgvList.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
dgvList.Columns(1).Visible = False
dgvList.Columns(2).Visible = True
dgvList.Columns(2).Width = 120
dgvList.Columns(2).HeaderText = "Start Date"
dgvList.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.Columns(3).Visible = True
dgvList.Columns(3).Width = 120
dgvList.Columns(3).HeaderText = "Due Date"
dgvList.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.Columns(4).Visible = False
dgvList.Columns(5).Visible = True
dgvList.Columns(5).Width = 90
dgvList.Columns(5).HeaderText = "Percent Complete"
dgvList.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.Columns(6).Visible = False
dgvList.Columns(7).Visible = False
dgvList.CurrentCell = Nothing
#End Region
End Sub
-
Jul 6th, 2023, 01:17 PM
#6
Re: DGV formatting
If you try to set styles for the whole grid, the same style with be used for every cell.
You can set styles for a row, or a column, or an individual cell.
Remember to do them in that order…
The grid
A row (or column)
An individual cell
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 6th, 2023, 02:54 PM
#7
Thread Starter
Fanatic Member
Re: DGV formatting
If you try to set styles for the whole grid, the same style with be used for every cell.
Yep. That's why I am working on a loop version of my SetState() method. I can tell you now it ain't easy. At least for me.
So this is what I have at the moment. I have run numerous times and even though it runs through the if statement and executes it correctly (everything goes through the appropriate branch), I still get no red forecolor for those cells that met the requirements of the if statement.
I am not sure why the 4 rows that met the criteria did not display Forecolor = color.DarkRed. Still trying to figure that out.
Code:
Private Sub DisplayDGV()
#Region "Color Settings"
BackColor = Color.SteelBlue
tlsList.BackColor = Color.LightSteelBlue
tlsList.ForeColor = Color.Black
dgvList.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black
dgvList.RowsDefaultCellStyle.BackColor = Color.LightSteelBlue
dgvList.BackgroundColor = Color.SteelBlue
dgvList.DefaultCellStyle.BackColor = Color.LightSteelBlue
'dgvList.DefaultCellStyle.ForeColor = Color.Black
#End Region
#Region "Default settings"
dgvList.Enabled = True
dgvList.EnableHeadersVisualStyles = False
dgvList.RowHeadersVisible = False
dgvList.AllowUserToAddRows = False
dgvList.AllowUserToDeleteRows = False
dgvList.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Text = ("Task List").ToString
#End Region
For i = 0 To RecordCount - 1
dgvList.Columns(0).Visible = True
dgvList.Columns(0).Width = 200
dgvList.Columns(0).HeaderText = "Task"
dgvList.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
If dgvList.Rows(i).Cells(7).Value = True Then
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.DarkRed
Else
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.Black
End If
dgvList.Columns(1).Visible = False
dgvList.Columns(2).Visible = True
dgvList.Columns(2).Width = 120
dgvList.Columns(2).HeaderText = "Start Date"
dgvList.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.Columns(3).Visible = True
dgvList.Columns(3).Width = 120
dgvList.Columns(3).HeaderText = "Due Date"
dgvList.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.Columns(4).Visible = False
dgvList.Columns(5).Width = 90
dgvList.Columns(5).HeaderText = "Percent Complete"
dgvList.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgvList.Columns(6).Visible = False
dgvList.Columns(7).Visible = False
Next
dgvList.CurrentCell = Nothing
End Sub
-
Jul 6th, 2023, 03:18 PM
#8
Re: DGV formatting
For individual rows or cells, instead of...
Code:
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.DarkRed
Try using...
Code:
dgvList.Rows(i).Style.ForeColor = Color.DarkRed
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 6th, 2023, 03:28 PM
#9
Re: DGV formatting
Correct... in fact, you shouldn't be setting ANY of the Default style settings ... but rather that row's Style... Including the Alignment.
-tg
-
Jul 6th, 2023, 03:42 PM
#10
Thread Starter
Fanatic Member
Re: DGV formatting
I tried that and it errored. It will not accept, .Style. Is there, perhaps some tool that I need to load to make that work? I can understand that one would want to not use the defaults.
-
Jul 6th, 2023, 03:59 PM
#11
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 6th, 2023, 05:12 PM
#12
Thread Starter
Fanatic Member
Re: DGV formatting
Here’s the MSDN documentation
I read the document and much of it was over my head. However, what I understood lead me to what you see below. Maybe I understood it, maybe not.
Anyway, there is no response in the DGV when this code is executed.
Code:
If dgvList.Rows(i).Cells(7).Value = True Then
dgvList.Rows(i).Cells(1).Style.ForeColor = Color.DarkRed
Else
dgvList.Rows(i).Cells(1).Style.ForeColor = Color.Black
End If
I really thought I had it that time.
-
Jul 6th, 2023, 05:35 PM
#13
Re: DGV formatting
Code:
If CBool(dgvList.Rows(i).Cells(7).Value) = True Then
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 6th, 2023, 05:38 PM
#14
-
Jul 6th, 2023, 07:18 PM
#15
Thread Starter
Fanatic Member
Re: DGV formatting
I followed up on the suggested reading list and there was one, from 2012 that resolved the exact same issue. Having said that, I had know idea what the code did, where it would go, or when one would call it.
The other thing I got out of it was that what I am experiencing, although not common, has been experienced by some number of others. It also appears to me that there is likely to be nothing wrong with my code, which would indicate to me that there might be something in the project, or some settings or property that could be causing this.
Assuming that my code is correct, never a good assumption since my eyes are so bad I can never be sure what I am actually reading (the eyes will soon be fixed, yea!), what other things might I look at that might cause this?
-
Jul 6th, 2023, 08:23 PM
#16
Thread Starter
Fanatic Member
Re: DGV formatting
I have finally made this work. I changed nothing, except changed the forecolor for a row, instead of a cell, as you can see below. The REM'd lines were some of the other attempts I made that did not work.
I have no explanation as to why this is works and the others do not, but the fact is that I would rather have the whole row instead of just the cell anyway. Win/win.
Code:
If CBool(dgvList.Rows(i).Cells(7).Value) = True Then
'dgvList.Rows(i).Cells(1).Style.ForeColor = Color.DarkRed
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.DarkRed
'dgvList(1, i).Style.ForeColor = Color.DarkRed
Else
'dgvList(1, i).Style.ForeColor = Color.Black
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.Black
'dgvList.Rows(i).Cells(1).Style.ForeColor = Color.Black
End If
-
Jul 7th, 2023, 03:42 AM
#17
Re: [RESOLVED] DGV formatting
It's probably just the order of operations you're using there...
Code:
If CBool(dgvList.Rows(i).Cells(7).Value) = True Then
'dgvList.Rows(i).Cells(1).Style.ForeColor = Color.DarkRed
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.DarkRed
'dgvList(1, i).Style.ForeColor = Color.DarkRed
Else
'dgvList(1, i).Style.ForeColor = Color.Black
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.Black
'dgvList.Rows(i).Cells(1).Style.ForeColor = Color.Black
End If
Try this...
Code:
If CBool(dgvList.Rows(i).Cells(7).Value) = True Then
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.Black
dgvList.Rows(i).Cells(1).Style.ForeColor = Color.DarkRed
Else
dgvList.Rows(i).DefaultCellStyle.ForeColor = Color.Black
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|