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. C#
  4. How do I format Column when a Gridview loads?

How do I format Column when a Gridview loads?

Scheduled Pinned Locked Moved C#
questioncsswpfwcf
9 Posts 3 Posters 0 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.
  • R Offline
    R Offline
    roman_s
    wrote on last edited by
    #1

    How do I format Column when a Gridview loads? Details: I have a grid named : SubcategoryGrid the grid is binded to a data binding source. there is a column in that grid(double type), I need to apply a simple conversion formula after the data binding completes what gridview argument do I use? or how would I go on about this?

    L T 2 Replies Last reply
    0
    • R roman_s

      How do I format Column when a Gridview loads? Details: I have a grid named : SubcategoryGrid the grid is binded to a data binding source. there is a column in that grid(double type), I need to apply a simple conversion formula after the data binding completes what gridview argument do I use? or how would I go on about this?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, have a look at the CellFormatting event. You could watch the value (e.Value) to be displayed and: 1. modify it, 2. or apply a special formatting (dgv.Columns[COL].DefaultCellStyle.Format="#,###";), 3. or set a special backcolor (e.CellStyle.BackColor=Color.Yellow; however this does not work for empty cells!). I have used some of the above in my CP Vanity[^] article. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      R 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, have a look at the CellFormatting event. You could watch the value (e.Value) to be displayed and: 1. modify it, 2. or apply a special formatting (dgv.Columns[COL].DefaultCellStyle.Format="#,###";), 3. or set a special backcolor (e.CellStyle.BackColor=Color.Yellow; however this does not work for empty cells!). I have used some of the above in my CP Vanity[^] article. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        R Offline
        R Offline
        roman_s
        wrote on last edited by
        #3

        Thanks, this is what I have: Class:

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
        }

        What args do I use in designer?? I tried this event to trigger cellformating: this.SubcategoryGrid.DataBindings += ........

        L 1 Reply Last reply
        0
        • R roman_s

          How do I format Column when a Gridview loads? Details: I have a grid named : SubcategoryGrid the grid is binded to a data binding source. there is a column in that grid(double type), I need to apply a simple conversion formula after the data binding completes what gridview argument do I use? or how would I go on about this?

          T Offline
          T Offline
          T M Gray
          wrote on last edited by
          #4

          Why does it have to happen after the binding? Is there some reason you can't use DataFormatString[^]?

          L 1 Reply Last reply
          0
          • R roman_s

            Thanks, this is what I have: Class:

            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
            }

            What args do I use in designer?? I tried this event to trigger cellformating: this.SubcategoryGrid.DataBindings += ........

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            :confused: :confused: :confused: AFAIK there are two distinct mechanisms, one to provide the data (databinding), one to display the data (formatting). The binding is whatever you want it to be. The formatting will fire the CellFormatting event if you wire it up, which you can do by double-clicking the event in the events list, or by adding a dataGridView1.CellFormatting+=dataGridView1_CellFormatting; statement in the form's constructor. As you already have the outline, you probably have done the former already. Now start manipulating the current cell inside your event handler, any way you want (You did not specify what kind of "simple conversion formula" you want). :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            R 1 Reply Last reply
            0
            • T T M Gray

              Why does it have to happen after the binding? Is there some reason you can't use DataFormatString[^]?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              That is for a WebUI thingy, isn't it? I believe the OP wants a WinForm with a DataGridView. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              T 1 Reply Last reply
              0
              • L Luc Pattyn

                :confused: :confused: :confused: AFAIK there are two distinct mechanisms, one to provide the data (databinding), one to display the data (formatting). The binding is whatever you want it to be. The formatting will fire the CellFormatting event if you wire it up, which you can do by double-clicking the event in the events list, or by adding a dataGridView1.CellFormatting+=dataGridView1_CellFormatting; statement in the form's constructor. As you already have the outline, you probably have done the former already. Now start manipulating the current cell inside your event handler, any way you want (You did not specify what kind of "simple conversion formula" you want). :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                R Offline
                R Offline
                roman_s
                wrote on last edited by
                #7

                Cellformating worked. Thank you!

                1 Reply Last reply
                0
                • L Luc Pattyn

                  That is for a WebUI thingy, isn't it? I believe the OP wants a WinForm with a DataGridView. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  T Offline
                  T Offline
                  T M Gray
                  wrote on last edited by
                  #8

                  Ahh he used the wrong control name. DataGridView and GridView are two completely different things. Using the correct term can vastly increase chances of finding useful info. A simple MSDN search of DataGridView gives the DataGridView page[^] which contains a list of common tasks with number 6 being How to: Customize Data Formatting in the Windows Forms DataGridView Control[^]

                  L 1 Reply Last reply
                  0
                  • T T M Gray

                    Ahh he used the wrong control name. DataGridView and GridView are two completely different things. Using the correct term can vastly increase chances of finding useful info. A simple MSDN search of DataGridView gives the DataGridView page[^] which contains a list of common tasks with number 6 being How to: Customize Data Formatting in the Windows Forms DataGridView Control[^]

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    T M Gray wrote:

                    Ahh he used the wrong control name

                    Happens all the time, we all get used to it. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    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