How to sort DataGrid using code?

A

a

I guess I could change the SELECT statement to ORDER BY, but that won't show
the little sort arrow on the DataGrid's column header.

What is the code to sort a Windows' DataGrid column? Simulate a user
clicking on a particular column header.

In my case, I want to sort by "Last Name" by default.

Thanks in advance.
 
T

Tim Wilson

You can do this by assigning a DataView into the DataSource property of the
DataGrid and setting the DataView's Sort property string.

DataView view = new DataView(myTable);
view.Sort = "LastName ASC";
dataGrid1.DataSource = view;

.... in addition you will need to make sure that the DataGrid's AllowSorting
property is set to "True", which is actually the default.
 
S

someone

Hi Tim,

Will that show the small arrow on the DataGrid's column header? Right now, I
have a DataSet assigned to the DataGrid's DataSource, and a DataTable (I
guess) to the DataGrid's DataMember. I'm new to all this. It seems there are
so many ways to pull data and display.

Thanks.
 
T

Tim Wilson

Yes. The method I showed will display the arrow on the column header. Just
create a DataView from the DataTable that you want to be displayed in the
DataGrid.
 
A

a

Hi Tim,

I did as you suggested, and got my sorting with the little arrow. The
problem now is that my Master/Detail (2 DataGrids) is broken. The Master
DataGrid DataSource is now a DataView and not a DataSet anymore.

Thanks.
 
T

Tim Wilson

How are you doing the Master/Detail work? So how are you getting the details
to show up in the detail grid when a record was changed in a different (the
master) grid?

One thing that you could try is to change the default view for the
appropriate table and then bind to the DataSet:
DataView view = dataSet1.Table["MyTable"].DefaultView;
view.Sort = "LastName ASC";
dataGrid1.SetDataBinding(dataSet1, "MyTable");
 
A

a

I have a Master DataGrid with:
DataSource = dataSetClientInfo (set at design time)
DataMember = Person (set at design time)

I have a Detail DataGrid with:
DataSource = dataSetClientInfo (set at design time)
DataMember = Person.PersonBooking (set at design time, PersonBooking is a
Relation)

I have code like:
this.sqlDataAdapterPerson.Fill(this.dataSetClientInfo);
this.sqlDataAdapterBooking.Fill(this.dataSetClientInfo);

After that, I set the DataGrid Column Styles in code, but I think this does
not really matter in our discussion here.

I tried your code below, it did not work. I also tried it with the design
time DataSource and DataMember removed for my Master DataGrid.

In your code below you have "view", but you don't use it in the DataGrid
binding, could that be a problem?

Thanks.


Tim Wilson said:
How are you doing the Master/Detail work? So how are you getting the details
to show up in the detail grid when a record was changed in a different (the
master) grid?

One thing that you could try is to change the default view for the
appropriate table and then bind to the DataSet:
DataView view = dataSet1.Table["MyTable"].DefaultView;
view.Sort = "LastName ASC";
dataGrid1.SetDataBinding(dataSet1, "MyTable");

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

a said:
Hi Tim,

I did as you suggested, and got my sorting with the little arrow. The
problem now is that my Master/Detail (2 DataGrids) is broken. The Master
DataGrid DataSource is now a DataView and not a DataSet anymore.

Thanks.


Right
now,
DataTable
(I property
of
 
T

Tim Wilson

Ok, I see what you're doing. I always create the Master/Detail in code, not
through the designer so this is somewhat out of my element. But, after much
kicking and screaming, I did find what looks to be a relatively simple
solution. Just place the code below in the constructor after the
InitializeComponent method call:

this.dataSet1.DefaultViewManager.DataViewSettings[this.dataTable1].Sort =
"Column2 ASC";

.... where "dataSet1" is your main DataSet, "dataTable1" is a DataTable
(either master or detail) in the DataSet, and "Column2" is the name of the
column in the table that you want to sort on (ASC or DESC).
In your code below you have "view", but you don't use it in the DataGrid
binding, could that be a problem?
You're right. I made a mistake. The code should really look like this:
dataSet1.Tables["Table1"].DefaultView.Sort = "Column2 ASC";
dataGrid1.SetDataBinding(dataSet1.Tables["Table1"], null);
But this would still mess up your master/detail done through the designer.
So just use the line up above and it should work out.

HTH
--
Tim Wilson
..Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

a said:
I have a Master DataGrid with:
DataSource = dataSetClientInfo (set at design time)
DataMember = Person (set at design time)

I have a Detail DataGrid with:
DataSource = dataSetClientInfo (set at design time)
DataMember = Person.PersonBooking (set at design time, PersonBooking is a
Relation)

I have code like:
this.sqlDataAdapterPerson.Fill(this.dataSetClientInfo);
this.sqlDataAdapterBooking.Fill(this.dataSetClientInfo);

After that, I set the DataGrid Column Styles in code, but I think this does
not really matter in our discussion here.

I tried your code below, it did not work. I also tried it with the design
time DataSource and DataMember removed for my Master DataGrid.

In your code below you have "view", but you don't use it in the DataGrid
binding, could that be a problem?

Thanks.


Tim Wilson said:
How are you doing the Master/Detail work? So how are you getting the details
to show up in the detail grid when a record was changed in a different (the
master) grid?

One thing that you could try is to change the default view for the
appropriate table and then bind to the DataSet:
DataView view = dataSet1.Table["MyTable"].DefaultView;
view.Sort = "LastName ASC";
dataGrid1.SetDataBinding(dataSet1, "MyTable");

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

Hi Tim,

I did as you suggested, and got my sorting with the little arrow. The
problem now is that my Master/Detail (2 DataGrids) is broken. The Master
DataGrid DataSource is now a DataView and not a DataSet anymore.

Thanks.


Yes. The method I showed will display the arrow on the column
header.
Just
create a DataView from the DataTable that you want to be displayed
in
the
DataGrid.

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}
Hi Tim,

Will that show the small arrow on the DataGrid's column header? Right
now,
I
have a DataSet assigned to the DataGrid's DataSource, and a
DataTable
(I
guess) to the DataGrid's DataMember. I'm new to all this. It seems there
are
so many ways to pull data and display.

Thanks.


You can do this by assigning a DataView into the DataSource property
of
the
DataGrid and setting the DataView's Sort property string.

DataView view = new DataView(myTable);
view.Sort = "LastName ASC";
dataGrid1.DataSource = view;

... in addition you will need to make sure that the DataGrid's
AllowSorting
property is set to "True", which is actually the default.

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

I guess I could change the SELECT statement to ORDER BY, but that
won't
show
the little sort arrow on the DataGrid's column header.

What is the code to sort a Windows' DataGrid column? Simulate
a
user
clicking on a particular column header.

In my case, I want to sort by "Last Name" by default.

Thanks in advance.
 
A

a

That worked great. Thanks a lot Tim.



Tim Wilson said:
Ok, I see what you're doing. I always create the Master/Detail in code, not
through the designer so this is somewhat out of my element. But, after much
kicking and screaming, I did find what looks to be a relatively simple
solution. Just place the code below in the constructor after the
InitializeComponent method call:

this.dataSet1.DefaultViewManager.DataViewSettings[this.dataTable1].Sort =
"Column2 ASC";

... where "dataSet1" is your main DataSet, "dataTable1" is a DataTable
(either master or detail) in the DataSet, and "Column2" is the name of the
column in the table that you want to sort on (ASC or DESC).
In your code below you have "view", but you don't use it in the DataGrid
binding, could that be a problem?
You're right. I made a mistake. The code should really look like this:
dataSet1.Tables["Table1"].DefaultView.Sort = "Column2 ASC";
dataGrid1.SetDataBinding(dataSet1.Tables["Table1"], null);
But this would still mess up your master/detail done through the designer.
So just use the line up above and it should work out.

HTH
--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

a said:
I have a Master DataGrid with:
DataSource = dataSetClientInfo (set at design time)
DataMember = Person (set at design time)

I have a Detail DataGrid with:
DataSource = dataSetClientInfo (set at design time)
DataMember = Person.PersonBooking (set at design time, PersonBooking is a
Relation)

I have code like:
this.sqlDataAdapterPerson.Fill(this.dataSetClientInfo);
this.sqlDataAdapterBooking.Fill(this.dataSetClientInfo);

After that, I set the DataGrid Column Styles in code, but I think this does
not really matter in our discussion here.

I tried your code below, it did not work. I also tried it with the design
time DataSource and DataMember removed for my Master DataGrid.

In your code below you have "view", but you don't use it in the DataGrid
binding, could that be a problem?

Thanks.


Tim Wilson said:
How are you doing the Master/Detail work? So how are you getting the details
to show up in the detail grid when a record was changed in a different (the
master) grid?

One thing that you could try is to change the default view for the
appropriate table and then bind to the DataSet:
DataView view = dataSet1.Table["MyTable"].DefaultView;
view.Sort = "LastName ASC";
dataGrid1.SetDataBinding(dataSet1, "MyTable");

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

Hi Tim,

I did as you suggested, and got my sorting with the little arrow. The
problem now is that my Master/Detail (2 DataGrids) is broken. The Master
DataGrid DataSource is now a DataView and not a DataSet anymore.

Thanks.


Yes. The method I showed will display the arrow on the column header.
Just
create a DataView from the DataTable that you want to be displayed in
the
DataGrid.

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}
Hi Tim,

Will that show the small arrow on the DataGrid's column header? Right
now,
I
have a DataSet assigned to the DataGrid's DataSource, and a DataTable
(I
guess) to the DataGrid's DataMember. I'm new to all this. It seems
there
are
so many ways to pull data and display.

Thanks.


You can do this by assigning a DataView into the DataSource property
of
the
DataGrid and setting the DataView's Sort property string.

DataView view = new DataView(myTable);
view.Sort = "LastName ASC";
dataGrid1.DataSource = view;

... in addition you will need to make sure that the DataGrid's
AllowSorting
property is set to "True", which is actually the default.

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

I guess I could change the SELECT statement to ORDER BY, but that
won't
show
the little sort arrow on the DataGrid's column header.

What is the code to sort a Windows' DataGrid column?
Simulate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top