Results 1 to 17 of 17

Thread: [RESOLVED] DGV formatting

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Resolved [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.

    Name:  screenshot.jpg
Views: 601
Size:  86.0 KB

    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?

  2. #2

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    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.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    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

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: DGV formatting

    Code:
    Instead of...
    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.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: DGV formatting


  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    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.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: DGV formatting

    Code:
    If CBool(dgvList.Rows(i).Cells(7).Value) = True Then

  14. #14

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    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?

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    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

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

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
  •  



Click Here to Expand Forum to Full Width