Can You Save the DataGridView Sort Order / Update the Underlying DB?

P

Patrick A

All,

When I save datagridview record changes, should the current sort
column and order save?

I have a datagridview I placed onto a form, and it is "fed" without
any sorting from a DB (bound datasource).

Changes to any cell in the datagridview save just fine, but if the
user sorts the datagridview by clicking on a column (as they like to
do), that does not save/update the DB. (And does not persist from
session to session.)

Is this by design?

If not, any suggstions as to what to verify or enable to make it work?

If so, any suggestions as to how I might code around this? Users want
sort to stay sorted, and some other parts of my app re-purpose the
data, which should also appear in the sorted order.

Thanks,

Patrick
 
A

Andrew Morton

Patrick said:
When I save datagridview record changes, should the current sort
column and order save?

Only if you save the current sort column and order somewhere and then sort
on that column automatically the next time a user retrieves that data. You
may want to do thar on a per-user basis.

The other parts of your app will have to use the saved sort column/order
too.

Data in a database are not inherently ordered in any way: {1,2,3,4} is the
same as {2,3,1,4} in a database because it is set-based. It is only when you
select data that you can choose an order for it.

HTH,

Andrew
 
P

Patrick A

Thanks Andrew!

I can save it on a per user basis, so that's no problem.

I like that it then becomes available elsewhere - I had not thought of
that.

I should also be able to save it to an ini file for use the next time
the user opens the app, correct?

(Just thinking out loud before breakfast.)

Patrick
 
A

Andrew Morton

Patrick said:
I can save it on a per user basis, so that's no problem.

I like that it then becomes available elsewhere - I had not thought of
that.

I should also be able to save it to an ini file for use the next time
the user opens the app, correct?

Presumably the database is (physically) the same one for all the users, so
it may make more sense to save it there rather than locally. That way, if
they log in (as themselves) on other computers, they will still have the
same state even if they don't have roaming profiles.

Andrew
 
P

Patrick A

Andrew,

Actually, the DB is a different one for all users. (It's a 200K MDB
file.) It is stored on their "H:" drive, a networked drive that maps
as the same letter wherever they log in.

But yes, I can write the value there or in the ini file, which is also
stored on their "H:" drive.
 
P

Patrick A

OK, I'm very close...but I'm getting poked in the eye by differing
types, and can't figure out how to convert
'System.String' to type 'System.Windows.Forms.DataGridViewColumn'

'My Declarations
Public Class MyGlobals
Public Shared TimersSortCol ' I have tried declaring this in
several ways...Nothing works on both sides.
Public Shared TimersSortOrd as String
End Class

'Getting the Values
'Get the TBL_TimersDataGridView sort order from the ini file
Dim strSortOrd As String =
MyGlobals.oIniFile.GetString("General", "SortOrd", "0")
MyGlobals.TimersSortOrd = strSortOrd
'returns a number

'Get the TBL_TimersDataGridView sort column from the ini file
Dim strSortCol As String =
MyGlobals.oIniFile.GetString("General", "SortCol", "0")
MyGlobals.TimersSortCol = strSortCol
'returns a number

'Using the Values
TBL_TimersDataGridView.Sort(MyGlobals.TimersSortCol,
MyGlobals.TimersSortOrd)

The error;

Unable to cast object of type 'System.String' to type
'System.Windows.Forms.DataGridViewColumn'.

Any suggestions?

Thanks,

Patrick
 
A

Andrew Morton

Patrick said:
OK, I'm very close...but I'm getting poked in the eye by differing
types, and can't figure out how to convert
'System.String' to type 'System.Windows.Forms.DataGridViewColumn'

Any suggestions?

Without looking into it any depth, I can suggest iterating over the columns
and using the one which has a name which is the same as the stored value.
There might be a more direct way.

Something like:

dim selectedCol as datagridviewcolumn
for each dgvc as datagridviewcolumn in TBL_TimersDataGridView.Columns
if dgvc.Name=whatever then
selectedCol=dgvc
exit for
end if
next

Andrew
 
P

Patrick A

Andrew,

Thanks for your reply, but I must be missing something - I'm not sure
how that sorts my column.

Can you provide a little more detail?

Patrick
 
A

Andrew Morton

Patrick said:
Without looking into it any depth, I can suggest iterating over the
columns and using the one which has a name which is the same as the
stored value. There might be a more direct way.

Something like:

dim selectedCol as datagridviewcolumn
for each dgvc as datagridviewcolumn in TBL_TimersDataGridView.Columns
if dgvc.Name=whatever then
[/QUOTE]

N.b: where "whatever" would be "MyGlobals.TimersSortCol"
Thanks for your reply, but I must be missing something - I'm not sure
how that sorts my column.

Can you provide a little more detail?

You see above where you needed a System.Windows.Forms.DataGridViewColumn
(DGVC) to tell it what to sort on, and you were getting the error that it
couldn't convert from "System.String"? Well, my idea was to find that
particular DGVC given its name (the string).

This part:
'Using the Values
TBL_TimersDataGridView.Sort(MyGlobals.TimersSortCol,
MyGlobals.TimersSortOrd)

Gave you the error
Unable to cast object of type 'System.String' to type
'System.Windows.Forms.DataGridViewColumn'.

because the first argument must be a DGVC.

Does that fill in the gap I left?

(The comments in your code "returns a number" are a bit unhelpful when it is
actually returning a string.)

Andrew
 

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