Datagridview formatting [modified]
-
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 "
-
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 "
Are you using AutoGenerateColumn=true? Why not define the columns as what you would like, before populating the data onto the DataGridView?
-
Are you using AutoGenerateColumn=true? Why not define the columns as what you would like, before populating the data onto the DataGridView?
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
-
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
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.
-
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.
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
-
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
-
Nevermind, you are not getting it.. You can define the columns _once_, before populating the datagridview with data.
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