Page 1 of 1

DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 1:50 am
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

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 3:09 am
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.

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 4:22 am
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?

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 4:26 am
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.

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 4:28 am
by jvierra
iamtj69 wrote: Fri Feb 14, 2020 4: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.

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 4:36 am
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'
}

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 4:53 am
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.

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 6:04 am
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.

Re: DataGridView - Column Text Alignment Issue

Posted: Fri Feb 14, 2020 6:42 am
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.