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. Windows Forms
  4. Datagridview formatting [modified]

Datagridview formatting [modified]

Scheduled Pinned Locked Moved Windows Forms
toolsperformancequestion
7 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.
  • M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #1

    This is a rant not a question. I have 2 DGVs with approx 1000 rows each on a single form, each has 14 columns and I need to format the alignment and text of the last 6 columns. I created this utility event that uses the cell format event to set the alignment based on the columns underlying data type (ValueType) and assigned it to both grids. Performance is not good, there is an appreciable delay when loading and filtering the grids, about 3-5 seconds

    	public static void dgCellFormat(object sender, DataGridViewCellFormattingEventArgs e)
    	{
    		DataGridViewColumn oCol = (sender as DataGridView).Columns\[e.ColumnIndex\];
    		string vt = oCol.ValueType.ToString().ToLower();
    		switch (vt)
    		{
    			case "system.int32":
    			case "system.int16":
    			case "system.int64":
    				e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    				//if the right 2 characters are not ID then add , to the value
    				if (!oCol.HeaderText.EndsWith("ID"))
    				{
    					e.CellStyle.Format = "#,#";
    				}
    				break;
    			case "system.decimal":
    				e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    				//if the right 2 characters are not ID then add , to the value
    				if (!oCol.HeaderText.EndsWith("ID"))
    				{
    					e.CellStyle.Format = "#,#.00";
    				}
    				break;
    			case "
    
    O 1 Reply Last reply
    0
    • M Mycroft Holmes

      This is a rant not a question. I have 2 DGVs with approx 1000 rows each on a single form, each has 14 columns and I need to format the alignment and text of the last 6 columns. I created this utility event that uses the cell format event to set the alignment based on the columns underlying data type (ValueType) and assigned it to both grids. Performance is not good, there is an appreciable delay when loading and filtering the grids, about 3-5 seconds

      	public static void dgCellFormat(object sender, DataGridViewCellFormattingEventArgs e)
      	{
      		DataGridViewColumn oCol = (sender as DataGridView).Columns\[e.ColumnIndex\];
      		string vt = oCol.ValueType.ToString().ToLower();
      		switch (vt)
      		{
      			case "system.int32":
      			case "system.int16":
      			case "system.int64":
      				e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
      				//if the right 2 characters are not ID then add , to the value
      				if (!oCol.HeaderText.EndsWith("ID"))
      				{
      					e.CellStyle.Format = "#,#";
      				}
      				break;
      			case "system.decimal":
      				e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
      				//if the right 2 characters are not ID then add , to the value
      				if (!oCol.HeaderText.EndsWith("ID"))
      				{
      					e.CellStyle.Format = "#,#.00";
      				}
      				break;
      			case "
      
      O Offline
      O Offline
      Obsidian713
      wrote on last edited by
      #2

      Are you using AutoGenerateColumn=true? Why not define the columns as what you would like, before populating the data onto the DataGridView?

      M 1 Reply Last reply
      0
      • O Obsidian713

        Are you using AutoGenerateColumn=true? Why not define the columns as what you would like, before populating the data onto the DataGridView?

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        This is a generic tool, take any DGV and any datatable and throw them together pass the result through this and you get a well formatted result. Customising a DGV for a specific datatable is not the goal, that way lies WPF. I loathe coding twice, so I often (nearly always) build a utility method that will service the bulk of my requirements. Then in the few time it needs tweaking I do the custom formatting. As an example I had almost forgotten how to bind a data control, I have had a utility that does it since dotnet 1.

        Never underestimate the power of human stupidity RAH

        D 1 Reply Last reply
        0
        • M Mycroft Holmes

          This is a generic tool, take any DGV and any datatable and throw them together pass the result through this and you get a well formatted result. Customising a DGV for a specific datatable is not the goal, that way lies WPF. I loathe coding twice, so I often (nearly always) build a utility method that will service the bulk of my requirements. Then in the few time it needs tweaking I do the custom formatting. As an example I had almost forgotten how to bind a data control, I have had a utility that does it since dotnet 1.

          Never underestimate the power of human stupidity RAH

          D Offline
          D Offline
          darkelv
          wrote on last edited by
          #4

          What I meant was before you assigned the dataset to the datagridview, build the columns base on your existing logic, versus look at each cell's data type and set the alignment for that cell(you are repeating this 1000 times or how many rows in the data table). It can still be generic to the data type of each column. After all, each column will have a specific data type, there's no point to do the alignment on every cell/row.

          M 1 Reply Last reply
          0
          • D darkelv

            What I meant was before you assigned the dataset to the datagridview, build the columns base on your existing logic, versus look at each cell's data type and set the alignment for that cell(you are repeating this 1000 times or how many rows in the data table). It can still be generic to the data type of each column. After all, each column will have a specific data type, there's no point to do the alignment on every cell/row.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            darkelv wrote:

            you are repeating this 1000 times or how many rows in the data table

            Your logic is impeccable - it matches mine precisely. So I set the format of the columns using the defaultcellstyle - this takes an order order of magnitude longer than setting the cell formats. This is the basis of my rant! Actually I think you will find it is doing it for the 100 or so cells that are currently on display.

            Never underestimate the power of human stupidity RAH

            D 1 Reply Last reply
            0
            • M Mycroft Holmes

              darkelv wrote:

              you are repeating this 1000 times or how many rows in the data table

              Your logic is impeccable - it matches mine precisely. So I set the format of the columns using the defaultcellstyle - this takes an order order of magnitude longer than setting the cell formats. This is the basis of my rant! Actually I think you will find it is doing it for the 100 or so cells that are currently on display.

              Never underestimate the power of human stupidity RAH

              D Offline
              D Offline
              darkelv
              wrote on last edited by
              #6

              Nevermind, you are not getting it.. You can define the columns _once_, before populating the datagridview with data.

              M 1 Reply Last reply
              0
              • D darkelv

                Nevermind, you are not getting it.. You can define the columns _once_, before populating the datagridview with data.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                Ah click, I've been binding the DGV, then changing the columns, and paying the price. So get the table, define the columns based on the table and then bind the datatable. Oh bloody hell, my utils does this it just doesn't do the datatype formatting - stupid frikking twat Thank you

                Never underestimate the power of human stupidity RAH

                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