How do I sort the columns (not rows) in a DataGrid?

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

I have a Windows Forms app in which I have a DataGrid control. I have a
custom class that has several properties exposed, and I have made this
class, or a collection thereof, the DataSource of the DataGrid. This neat
feature of the DataGrid allows the properties to be displayed in a
Collection of this class.

Now, how do I order the columns (not to be confused with the rows, which is
managed by the collection)? Is there a simple process to do this, while
still using my simple class and not resorting to ADO.NET or XML? I would
prefer not to move to ADO.NET or XML, but if it's my only option I'd like to
know.

Jon
 
I think you have to set your own DataGridTableStyle:

DataGrid.DataGridTableStyle.GridColumnStyles.Clear(); //Remove all Columns
DataGrid.DataGridTableStyle.GridColumnStyles.AddRange(new
DataGrisColumnStyle[]{ .... }); // Set the columns to display and also
their order

And add the columns in the order that you want
 
Thanks!

Zürcher See said:
I think you have to set your own DataGridTableStyle:

DataGrid.DataGridTableStyle.GridColumnStyles.Clear(); //Remove all Columns
DataGrid.DataGridTableStyle.GridColumnStyles.AddRange(new
DataGrisColumnStyle[]{ .... }); // Set the columns to display and also
their order

And add the columns in the order that you want

Jon Davis said:
I have a Windows Forms app in which I have a DataGrid control. I have a
custom class that has several properties exposed, and I have made this
class, or a collection thereof, the DataSource of the DataGrid. This neat
feature of the DataGrid allows the properties to be displayed in a
Collection of this class.

Now, how do I order the columns (not to be confused with the rows, which is
managed by the collection)? Is there a simple process to do this, while
still using my simple class and not resorting to ADO.NET or XML? I would
prefer not to move to ADO.NET or XML, but if it's my only option I'd
like
to
know.

Jon
 
Unfortunately, there is no DataGridTableStyle property in the Windows Forms
DataGrid. There is a TableStyles property, but it is a collection. If I go
MyDataGrid.TableStyles[0].GridColumnStyles.AddRange(new
DataGridColumnStyle[] {.... I don't know what to put here, and
DataGridColumnStyle's constructor is not accessible.

Jon

Jon Davis said:
Thanks!

Zürcher See said:
I think you have to set your own DataGridTableStyle:

DataGrid.DataGridTableStyle.GridColumnStyles.Clear(); //Remove all Columns
DataGrid.DataGridTableStyle.GridColumnStyles.AddRange(new
DataGrisColumnStyle[]{ .... }); // Set the columns to display and also
their order

And add the columns in the order that you want

Jon Davis said:
I have a Windows Forms app in which I have a DataGrid control. I have a
custom class that has several properties exposed, and I have made this
class, or a collection thereof, the DataSource of the DataGrid. This neat
feature of the DataGrid allows the properties to be displayed in a
Collection of this class.

Now, how do I order the columns (not to be confused with the rows,
which
is
managed by the collection)? Is there a simple process to do this, while
still using my simple class and not resorting to ADO.NET or XML? I would
prefer not to move to ADO.NET or XML, but if it's my only option I'd
like
to
know.

Jon
 
In fact, TableStyles[0] fails because there are none.

Jon Davis said:
Unfortunately, there is no DataGridTableStyle property in the Windows Forms
DataGrid. There is a TableStyles property, but it is a collection. If I go
MyDataGrid.TableStyles[0].GridColumnStyles.AddRange(new
DataGridColumnStyle[] {.... I don't know what to put here, and
DataGridColumnStyle's constructor is not accessible.

Jon

Jon Davis said:
Thanks!

Zürcher See said:
I think you have to set your own DataGridTableStyle:

DataGrid.DataGridTableStyle.GridColumnStyles.Clear(); //Remove all Columns
DataGrid.DataGridTableStyle.GridColumnStyles.AddRange(new
DataGrisColumnStyle[]{ .... }); // Set the columns to display and also
their order

And add the columns in the order that you want

I have a Windows Forms app in which I have a DataGrid control. I
have
 
Back
Top