PC Review


Reply
Thread Tools Rate Thread

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

 
 
Patrick A
Guest
Posts: n/a
 
      26th Mar 2010
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
 
Reply With Quote
 
 
 
 
Andrew Morton
Guest
Posts: n/a
 
      26th Mar 2010
Patrick A wrote:
> 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


 
Reply With Quote
 
Patrick A
Guest
Posts: n/a
 
      26th Mar 2010
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

On Mar 26, 4:57*am, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
> Patrick A wrote:
> > 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


 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      26th Mar 2010
Patrick A wrote:
> 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


 
Reply With Quote
 
Patrick A
Guest
Posts: n/a
 
      26th Mar 2010
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.


storyOn Mar 26, 7:57*am, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
> Patrick A wrote:
> > 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


 
Reply With Quote
 
Patrick A
Guest
Posts: n/a
 
      26th Mar 2010
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

 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      29th Mar 2010
Patrick A wrote:
> 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


 
Reply With Quote
 
Patrick A
Guest
Posts: n/a
 
      31st Mar 2010
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

On Mar 29, 6:00*am, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
> Patrick A wrote:
> > 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


 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      31st Mar 2010
Patrick A wrote:

> On Mar 29, 6:00 am, "Andrew Morton"
>> Patrick A wrote:
>>> 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


N.b: where "whatever" would be "MyGlobals.TimersSortCol"

>> selectedCol=dgvc
>> exit for
>> end if
>> next
>>


> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Coordinating the sort order in a report with the underlying query Paul Microsoft Access Reports 2 27th Aug 2009 02:49 PM
Re: How to get a DATAGRIDVIEW COLUMN to sort in DATE Order??? ShaneO Microsoft VB .NET 0 22nd Jan 2009 06:02 AM
Re: How to get a DATAGRIDVIEW COLUMN to sort in DATE Order??? Rich P Microsoft VB .NET 0 21st Jan 2009 11:14 PM
How to force DataGridView to update itself in compliance with underlying DataSource ? Oleg Subachev Microsoft C# .NET 2 7th Nov 2008 06:03 AM
VB2005 - Sort DataGridView on Text (FormattedValue) of a ComboBoxcolumn instead of underlying value? Matt Microsoft VB .NET 4 23rd Jun 2006 12:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:03 PM.