DataGridView - Column Text Alignment Issue

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
iamtj69
Posts: 11
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Thu Sep 14, 2017 2:05 am

DataGridView - Column Text Alignment Issue

Post by iamtj69 »

Hello,

I am trying to "Middle Right" align text in a column in a DataGridView.

I am trying the following:

Code: Select all

$datagridview1.Columns["total"].DefaultCellStyle.Alignment = 'MiddleRight'
$datagridview1.Columns["total"].HeaderCell.Style.Alignment = 'MiddleRight'
However i am receiving the following:

ERROR: The property 'Alignment' cannot be found on this object. Verify that the property exists and can be set.

I am struggling to see why this is failing. Has anyone else had a similar issue?

The column contains integers.

Powershell Studio 2019 (v5.6.167) 64 Bit

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView - Column Text Alignment Issue

Post by jvierra »

You cannot set that on individual columns. You can only set the global column alignment for all columns.

Because the style object is set by type the properties of the style object appear when you look but they are inherited from the global template and cannot be set. The property does not exist on individual columns.

User avatar
iamtj69
Posts: 11
Joined: Thu Sep 14, 2017 2:05 am

Re: DataGridView - Column Text Alignment Issue

Post by iamtj69 »

Ah, ok that makes sense.

Does something similar apply for disabling "sort". This is something else i have been struggling with. How can i disable 'sort' for an entire grid?

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView - Column Text Alignment Issue

Post by jvierra »

Your issue seems to be because you are trying to change the style after the grid has been loaded. Set teh style for the cells only before the grid is loaded and displayed.
You can also probably do this after the grid is loaded by creating a new style object and adding assigning it to the DefaultCellStyle property.

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView - Column Text Alignment Issue

Post by jvierra »

iamtj69 wrote:
Fri Feb 14, 2020 5:22 am
Does something similar apply for disabling "sort". This is something else i have been struggling with. How can i disable 'sort' for an entire grid?
There is no sort for a grid. The data displays however it is loaded.

User avatar
iamtj69
Posts: 11
Joined: Thu Sep 14, 2017 2:05 am

Re: DataGridView - Column Text Alignment Issue

Post by iamtj69 »

How do you recommend i disable the ability to sort on all columns?

I am trying following with no joy:

Code: Select all

foreach ($col in $datagridview1.Columns)
{
	$col.SortMode = 'NotSortable'
}

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView - Column Text Alignment Issue

Post by jvierra »

That doesn't "unsort" data. There is no way to "unsort" data. That just tells the grid how it will be sorted. If you choose "Automatic" then the grid will be sorted on that column. If the grid is already loaded it will not be changed when you change the value. To autosort you need to use a DataTable and then there is still no way to unsort the data.
Think about it. It is impossible to ever unsort something. We can remember some previous order and sort it by that order but we still have to sort it to get there.
Once you sort by any column your only option is to sort by a different column. With a DataTable we can apply a data sort to the view and we can remove the sort filter on the view. This would return the grid to its default view which may be what you are asking.

User avatar
iamtj69
Posts: 11
Joined: Thu Sep 14, 2017 2:05 am

Re: DataGridView - Column Text Alignment Issue

Post by iamtj69 »

Sorry, maybe i wasn't being very clear.

I was just trying to disable the ability for a user to sort any column in the datagridview (i have achieved this now), i wasn't trying to "unsort" any columns.

The data comes pre-sorted from an SQL database as a dataset (collection of datatables). I then populate several Datagridviews on a form with these tables.

One table in particular has over 50,000 rows, so can hang if someone tries to sort by a column. I simply wanted to disable the ability to sort.

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView - Column Text Alignment Issue

Post by jvierra »

Columns do not sort automatically. If you design the grid to to set the column to NotSortable in the designer then they won't be clickable. If you load the grid from a DataTable then try to disable the mode it will cause the columns to be reset because they are being "de-buttoned" but it will cause the columns to be non-sortable.

Locked