Formatting cells of a DataGridView best place for the code?
Posted: Tue Aug 06, 2019 11:39 pm
Hi Everyone,
Now that I have really dived into the Windows Forms usage through PowerShell Studio I have a question in regards to the best code loaction placement for formatting the cell contents in a DataGridView based on the cells value.
The place where I current have the formatting and the setting of the cell colors is in the GREAT SAPIEN function: Update-DataGridView
But I also need and want to have the column sorting functionality of the DataTable so I am also using that other GREAT SAPEIN function: ConvertTo-DataTable
When I first load my DataGridView by converting it to a DataTable using ConvertTo-DataTable and then updating the DataGridView using Update-DataGridView the coloring logic that I added to the bottom of the later function works just fine until I click on one of the column headers to re-sort the columns to the new desired order.
I have tried to look at the hundereds of Events and Methods and such that the DataGridView offers for handling these object and I don't have all the time needed to test through each and every one of these until I find the right one.
So can someone help me to find the Right Event or method that I should use when I want the cell Contents to be conditionally formatted based on the cells value?
I don't want to post the actual code here due to sensitive Information in it. I don't actually use the column numbers for the coloring as listed below but the column header text so that the column number does NOT matter.
Examples:
For columns 6, 7, 8 and 9 the default colors are Background 'White', Foreground 'Black'.
These four columns have an integer value that I would also like to have formatted for the display purposes like a string "{0:N0}" but for the sorting it should use the integer value because string sorting of such value is not nice at all as you know.
I have set the cell Alignment to: 'MiddleRight'
If column 6 has a value of 0 then the foreground Color should be 'Green' with the default Background Color, if it is greater than zero then the normal default Colors.
If column 7 is greater than 0 then the Background Color should be 'Green' and the Foreground Color 'White',
If column 8 if the value is greater than 0 then the Background Color should be 'Yellow' foreground 'Black',
If column 9 if the value is greater than o then the Background Color should be 'Red' and the foreground Color 'White'.
Thanks in Advance...
Now that I have really dived into the Windows Forms usage through PowerShell Studio I have a question in regards to the best code loaction placement for formatting the cell contents in a DataGridView based on the cells value.
The place where I current have the formatting and the setting of the cell colors is in the GREAT SAPIEN function: Update-DataGridView
But I also need and want to have the column sorting functionality of the DataTable so I am also using that other GREAT SAPEIN function: ConvertTo-DataTable
When I first load my DataGridView by converting it to a DataTable using ConvertTo-DataTable and then updating the DataGridView using Update-DataGridView the coloring logic that I added to the bottom of the later function works just fine until I click on one of the column headers to re-sort the columns to the new desired order.
I have tried to look at the hundereds of Events and Methods and such that the DataGridView offers for handling these object and I don't have all the time needed to test through each and every one of these until I find the right one.
So can someone help me to find the Right Event or method that I should use when I want the cell Contents to be conditionally formatted based on the cells value?
I don't want to post the actual code here due to sensitive Information in it. I don't actually use the column numbers for the coloring as listed below but the column header text so that the column number does NOT matter.
Examples:
For columns 6, 7, 8 and 9 the default colors are Background 'White', Foreground 'Black'.
These four columns have an integer value that I would also like to have formatted for the display purposes like a string "{0:N0}" but for the sorting it should use the integer value because string sorting of such value is not nice at all as you know.
I have set the cell Alignment to: 'MiddleRight'
If column 6 has a value of 0 then the foreground Color should be 'Green' with the default Background Color, if it is greater than zero then the normal default Colors.
If column 7 is greater than 0 then the Background Color should be 'Green' and the Foreground Color 'White',
If column 8 if the value is greater than 0 then the Background Color should be 'Yellow' foreground 'Black',
If column 9 if the value is greater than o then the Background Color should be 'Red' and the foreground Color 'White'.
Thanks in Advance...