Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. DataGridView.Column("name").DefaultCellStyle.Format applied to all columns (it shouldn't be)

DataGridView.Column("name").DefaultCellStyle.Format applied to all columns (it shouldn't be)

Scheduled Pinned Locked Moved Visual Basic
helpquestionlearning
13 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johan Hakkesteegt
    wrote on last edited by
    #1

    Hi, I have the following method in my (Windows) application to format a report in a DataGridView. The report has a variable amount of columns.

    Private Sub FormatColumns(ByVal dgv As DataGridView)
    Try
    For Each clm As DataGridViewColumn In dgv.Columns
    Select Case True
    Case clm.Name Like "*Konserni*"
    'a string value
    clm.Width = 150
    Case clm.Name Like "*Ketju*"
    'a string value
    clm.Width = 150
    Case clm.Name Like "*Myyjä*"
    'a string value
    clm.Width = 150
    Case clm.Name Like "*Myynti*"
    'a monetary value
    clm.Width = 100
    clm.DefaultCellStyle.Format = "c"
    Case clm.Name Like "*Budjetti*"
    'a monetary value
    clm.Width = 100
    clm.DefaultCellStyle.Format = "c"
    Case clm.Name.ToLower Like "*kate*"
    'a percentage value
    clm.Width = 120
    clm.DefaultCellStyle.Format = "P"
    Case Else
    clm.Visible = False
    End Select
    Next
    Catch ex As Exception
    EC(ex)
    End Try
    End Sub

    When I run the report, and apply this method all columns are formatted as percentages (except the string values of course). It is supposed to format each column differently, depending on the type of value. What am I doing wrong, or what did I misunderstand about (the use of) DefaultCellStyle ? Any help appreciated. Regards, Johan

    My advice is free, and you may get what you paid for.

    D L 2 Replies Last reply
    0
    • J Johan Hakkesteegt

      Hi, I have the following method in my (Windows) application to format a report in a DataGridView. The report has a variable amount of columns.

      Private Sub FormatColumns(ByVal dgv As DataGridView)
      Try
      For Each clm As DataGridViewColumn In dgv.Columns
      Select Case True
      Case clm.Name Like "*Konserni*"
      'a string value
      clm.Width = 150
      Case clm.Name Like "*Ketju*"
      'a string value
      clm.Width = 150
      Case clm.Name Like "*Myyjä*"
      'a string value
      clm.Width = 150
      Case clm.Name Like "*Myynti*"
      'a monetary value
      clm.Width = 100
      clm.DefaultCellStyle.Format = "c"
      Case clm.Name Like "*Budjetti*"
      'a monetary value
      clm.Width = 100
      clm.DefaultCellStyle.Format = "c"
      Case clm.Name.ToLower Like "*kate*"
      'a percentage value
      clm.Width = 120
      clm.DefaultCellStyle.Format = "P"
      Case Else
      clm.Visible = False
      End Select
      Next
      Catch ex As Exception
      EC(ex)
      End Try
      End Sub

      When I run the report, and apply this method all columns are formatted as percentages (except the string values of course). It is supposed to format each column differently, depending on the type of value. What am I doing wrong, or what did I misunderstand about (the use of) DefaultCellStyle ? Any help appreciated. Regards, Johan

      My advice is free, and you may get what you paid for.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      I doubt there is a problem with DefaultCellStyle. The DefaultCellStyle only applies to the column you set it in, not to all columns. Set a breakpoint on the For Each line and step through the code in the debugger, line by line, so you can see what the code is seeing and doing. I really wouldn't use the ``Like`` operator here since it's intended for pattern matching, not outright equality.

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      J 2 Replies Last reply
      0
      • J Johan Hakkesteegt

        Hi, I have the following method in my (Windows) application to format a report in a DataGridView. The report has a variable amount of columns.

        Private Sub FormatColumns(ByVal dgv As DataGridView)
        Try
        For Each clm As DataGridViewColumn In dgv.Columns
        Select Case True
        Case clm.Name Like "*Konserni*"
        'a string value
        clm.Width = 150
        Case clm.Name Like "*Ketju*"
        'a string value
        clm.Width = 150
        Case clm.Name Like "*Myyjä*"
        'a string value
        clm.Width = 150
        Case clm.Name Like "*Myynti*"
        'a monetary value
        clm.Width = 100
        clm.DefaultCellStyle.Format = "c"
        Case clm.Name Like "*Budjetti*"
        'a monetary value
        clm.Width = 100
        clm.DefaultCellStyle.Format = "c"
        Case clm.Name.ToLower Like "*kate*"
        'a percentage value
        clm.Width = 120
        clm.DefaultCellStyle.Format = "P"
        Case Else
        clm.Visible = False
        End Select
        Next
        Catch ex As Exception
        EC(ex)
        End Try
        End Sub

        When I run the report, and apply this method all columns are formatted as percentages (except the string values of course). It is supposed to format each column differently, depending on the type of value. What am I doing wrong, or what did I misunderstand about (the use of) DefaultCellStyle ? Any help appreciated. Regards, Johan

        My advice is free, and you may get what you paid for.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I think "SELECT CASE TRUE" would always select the same case. Did you want to switch on the name?

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        J 1 Reply Last reply
        0
        • D Dave Kreskowiak

          I doubt there is a problem with DefaultCellStyle. The DefaultCellStyle only applies to the column you set it in, not to all columns. Set a breakpoint on the For Each line and step through the code in the debugger, line by line, so you can see what the code is seeing and doing. I really wouldn't use the ``Like`` operator here since it's intended for pattern matching, not outright equality.

          A guide to posting questions on CodeProject

          Click this: Asking questions is a skill. Seriously, do it.
          Dave Kreskowiak

          J Offline
          J Offline
          Johan Hakkesteegt
          wrote on last edited by
          #4

          Hi Dave, Thanks, I will try step through. Regards, Johan P.S. In the real code I am matching a pattern, it appears that the stars were dropped by the codeproject editor.

          My advice is free, and you may get what you paid for.

          1 Reply Last reply
          0
          • L Lost User

            I think "SELECT CASE TRUE" would always select the same case. Did you want to switch on the name?

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            J Offline
            J Offline
            Johan Hakkesteegt
            wrote on last edited by
            #5

            Hi, In VB the switch works like this. In other words, "Select the first case which' outcome conforms to the main statement". It appears that the codeproject editor dropped the stars from the pattern matching. Regards, Johan

            My advice is free, and you may get what you paid for.

            L 1 Reply Last reply
            0
            • D Dave Kreskowiak

              I doubt there is a problem with DefaultCellStyle. The DefaultCellStyle only applies to the column you set it in, not to all columns. Set a breakpoint on the For Each line and step through the code in the debugger, line by line, so you can see what the code is seeing and doing. I really wouldn't use the ``Like`` operator here since it's intended for pattern matching, not outright equality.

              A guide to posting questions on CodeProject

              Click this: Asking questions is a skill. Seriously, do it.
              Dave Kreskowiak

              J Offline
              J Offline
              Johan Hakkesteegt
              wrote on last edited by
              #6

              Ok, so I tried step through, and the only thing unexpected that I see, is that once the Format property has been set for a column in the loop, that setting is maintained through subsequent loops, until changed (again). So unless this is a bug or some cache issue in the debugging process, showing the value of a property incorrectly, I suppose it is possible that properties of the DefaultCellStyle, accessed through the column, get set for all columns in the DataGridView, not just for that specific column. I have worked around this by using:

              DataGridViewCell.Style.Format

              , but it takes noticeably longer now that I need to apply formatting one cell at a time.

              My advice is free, and you may get what you paid for.

              D 1 Reply Last reply
              0
              • J Johan Hakkesteegt

                Hi, In VB the switch works like this. In other words, "Select the first case which' outcome conforms to the main statement". It appears that the codeproject editor dropped the stars from the pattern matching. Regards, Johan

                My advice is free, and you may get what you paid for.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Johan Hakkesteegt wrote:

                In VB the switch works like this.

                Never seen it before; nice to learn something new :thumbsup:

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                1 Reply Last reply
                0
                • J Johan Hakkesteegt

                  Ok, so I tried step through, and the only thing unexpected that I see, is that once the Format property has been set for a column in the loop, that setting is maintained through subsequent loops, until changed (again). So unless this is a bug or some cache issue in the debugging process, showing the value of a property incorrectly, I suppose it is possible that properties of the DefaultCellStyle, accessed through the column, get set for all columns in the DataGridView, not just for that specific column. I have worked around this by using:

                  DataGridViewCell.Style.Format

                  , but it takes noticeably longer now that I need to apply formatting one cell at a time.

                  My advice is free, and you may get what you paid for.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Nope. ``DefaultCellStyle`` applies ONLY to the column that it is set on. Also, there is no debugging "cache". I coudln't tell you what's going on because we simply don't have enough information on your code and how the rest of the grid is setup.

                  A guide to posting questions on CodeProject

                  Click this: Asking questions is a skill. Seriously, do it.
                  Dave Kreskowiak

                  J 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Nope. ``DefaultCellStyle`` applies ONLY to the column that it is set on. Also, there is no debugging "cache". I coudln't tell you what's going on because we simply don't have enough information on your code and how the rest of the grid is setup.

                    A guide to posting questions on CodeProject

                    Click this: Asking questions is a skill. Seriously, do it.
                    Dave Kreskowiak

                    J Offline
                    J Offline
                    Johan Hakkesteegt
                    wrote on last edited by
                    #9

                    Hi Dave, Would it suffice if I tell you the steps (in pseudo code) ? 1. Open Visual Studio 2012 Express 2. Start new Windows Forms project (.NET 4) 3. Drag DataGridView and button controls onto Window 4. Press button = - Use SqlCommand to run MS SQL stored procedure that returns a table - Use SqlDataAdapter to enter data into DataSet 5. BindingSource.DataSource = DataSet 6. DataGridView.DataSource = BindingSource 7. Run Method as in my original post Regards, Johan

                    My advice is free, and you may get what you paid for.

                    D 1 Reply Last reply
                    0
                    • J Johan Hakkesteegt

                      Hi Dave, Would it suffice if I tell you the steps (in pseudo code) ? 1. Open Visual Studio 2012 Express 2. Start new Windows Forms project (.NET 4) 3. Drag DataGridView and button controls onto Window 4. Press button = - Use SqlCommand to run MS SQL stored procedure that returns a table - Use SqlDataAdapter to enter data into DataSet 5. BindingSource.DataSource = DataSet 6. DataGridView.DataSource = BindingSource 7. Run Method as in my original post Regards, Johan

                      My advice is free, and you may get what you paid for.

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Johan Hakkesteegt wrote:

                      Would it suffice if I tell you the steps (in pseudo code) ?

                      No, because that is what you THINK the code is doing. The real code is required to show what the code is ACTUALLY doing.

                      A guide to posting questions on CodeProject

                      Click this: Asking questions is a skill. Seriously, do it.
                      Dave Kreskowiak

                      J 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        Johan Hakkesteegt wrote:

                        Would it suffice if I tell you the steps (in pseudo code) ?

                        No, because that is what you THINK the code is doing. The real code is required to show what the code is ACTUALLY doing.

                        A guide to posting questions on CodeProject

                        Click this: Asking questions is a skill. Seriously, do it.
                        Dave Kreskowiak

                        J Offline
                        J Offline
                        Johan Hakkesteegt
                        wrote on last edited by
                        #11

                        Hi Dave, You were right (as usual). I found that I was calling another method (see below) before using the one I posted originally. When I commented this one out, the first method worked as expected. Looking at it though, I don't see how this method caused the unexpected behavior. As far as I can tell, it is pretty much doing the same thing.

                        Public Sub FormatTable(ByVal dgv As DataGridView)
                        Dim cs_Default As New DataGridViewCellStyle(dgv.DefaultCellStyle)
                        Dim cs_Money As New DataGridViewCellStyle(cs_Default)
                        Dim cs_Date As New DataGridViewCellStyle(cs_Default)
                        Dim cs_String As New DataGridViewCellStyle(cs_Default)
                        Dim cs_Integer As New DataGridViewCellStyle(cs_Default)
                        Try
                        With cs_Date
                        .Alignment = DataGridViewContentAlignment.MiddleLeft
                        End With
                        With cs_Money
                        .Alignment = DataGridViewContentAlignment.MiddleRight
                        .Format = "F"
                        End With
                        With cs_Integer
                        .Alignment = DataGridViewContentAlignment.MiddleRight
                        .Format = "N0"
                        End With
                        With cs_String
                        .Alignment = DataGridViewContentAlignment.MiddleLeft
                        End With
                        For Each clm As DataGridViewColumn In dgv.Columns
                        Select Case clm.ValueType
                        Case Type_Double, Type_Long, Type_Decimal
                        clm.DefaultCellStyle = cs_Money
                        Case Type_Integer
                        clm.DefaultCellStyle = cs_Integer
                        Case Type_String
                        clm.DefaultCellStyle = cs_String
                        End Select
                        Next
                        Catch ex As Exception
                        EC(ex)
                        End Try
                        End Sub

                        Regards, Johan

                        My advice is free, and you may get what you paid for.

                        D 1 Reply Last reply
                        0
                        • J Johan Hakkesteegt

                          Hi Dave, You were right (as usual). I found that I was calling another method (see below) before using the one I posted originally. When I commented this one out, the first method worked as expected. Looking at it though, I don't see how this method caused the unexpected behavior. As far as I can tell, it is pretty much doing the same thing.

                          Public Sub FormatTable(ByVal dgv As DataGridView)
                          Dim cs_Default As New DataGridViewCellStyle(dgv.DefaultCellStyle)
                          Dim cs_Money As New DataGridViewCellStyle(cs_Default)
                          Dim cs_Date As New DataGridViewCellStyle(cs_Default)
                          Dim cs_String As New DataGridViewCellStyle(cs_Default)
                          Dim cs_Integer As New DataGridViewCellStyle(cs_Default)
                          Try
                          With cs_Date
                          .Alignment = DataGridViewContentAlignment.MiddleLeft
                          End With
                          With cs_Money
                          .Alignment = DataGridViewContentAlignment.MiddleRight
                          .Format = "F"
                          End With
                          With cs_Integer
                          .Alignment = DataGridViewContentAlignment.MiddleRight
                          .Format = "N0"
                          End With
                          With cs_String
                          .Alignment = DataGridViewContentAlignment.MiddleLeft
                          End With
                          For Each clm As DataGridViewColumn In dgv.Columns
                          Select Case clm.ValueType
                          Case Type_Double, Type_Long, Type_Decimal
                          clm.DefaultCellStyle = cs_Money
                          Case Type_Integer
                          clm.DefaultCellStyle = cs_Integer
                          Case Type_String
                          clm.DefaultCellStyle = cs_String
                          End Select
                          Next
                          Catch ex As Exception
                          EC(ex)
                          End Try
                          End Sub

                          Regards, Johan

                          My advice is free, and you may get what you paid for.

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          There is probably something in between this method and the other one. Having multiple methods to format a grid is a bad idea because of the problem you're running into, though it's, sadly, kind of common. If I have multiple grids I need formatted differently, but overall styled the same, I normally create my own DGV class, inheriting from the .NET DGV, setup the base styles common to all grids, then use that I my forms. When I need to setup formatting, I have a method on the form that hosts the grid explicitly setting up the one grid instance with the things that are over and above the items setup in the base grid. This sometimes results in a duplication of code but I consider that a small price to pay for flexibility when I need to change a grid here and there. Now, you might want to step through this code and the other code. It would appear that you're replacing the entire style of a column instead of making changes to it. If this code is called to setup your grid and then you call the other method to further change it you could be replacing the styles from one method with the styles from another instead of making changes to the style that is in place.

                          A guide to posting questions on CodeProject

                          Click this: Asking questions is a skill. Seriously, do it.
                          Dave Kreskowiak

                          J 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            There is probably something in between this method and the other one. Having multiple methods to format a grid is a bad idea because of the problem you're running into, though it's, sadly, kind of common. If I have multiple grids I need formatted differently, but overall styled the same, I normally create my own DGV class, inheriting from the .NET DGV, setup the base styles common to all grids, then use that I my forms. When I need to setup formatting, I have a method on the form that hosts the grid explicitly setting up the one grid instance with the things that are over and above the items setup in the base grid. This sometimes results in a duplication of code but I consider that a small price to pay for flexibility when I need to change a grid here and there. Now, you might want to step through this code and the other code. It would appear that you're replacing the entire style of a column instead of making changes to it. If this code is called to setup your grid and then you call the other method to further change it you could be replacing the styles from one method with the styles from another instead of making changes to the style that is in place.

                            A guide to posting questions on CodeProject

                            Click this: Asking questions is a skill. Seriously, do it.
                            Dave Kreskowiak

                            J Offline
                            J Offline
                            Johan Hakkesteegt
                            wrote on last edited by
                            #13

                            Thanks Dave, I will look into that. Thanks for the help !

                            My advice is free, and you may get what you paid for.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups