Discussion:
Sorting DBGrid ?
(too old to reply)
Sasan
2008-07-26 07:51:01 UTC
Permalink
Hello,

I create OnTitle click for sorting dbgrid but it has a bug:

If all columns can not seen in grid and user click column that scrolled to
see it, grid sort by that filed also
columns scrolled to first column !

How can prevent from scrolling after sorting ?

/////////////////
// CODE

//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
bool Order;
AnsiString Str;
TCustomADODataSet * Dataset;

Dataset = dynamic_cast <TCustomADODataSet
*>(Column->Grid->DataSource->DataSet);
Order = Pos(" DESC", Dataset->Sort) == 0 ? true : false;

Order = Pos(Column->Field->FieldName, Dataset->Sort) == 1 ? !Order : Order;
Str = ::Format("%s %s", ARRAYOFCONST((AnsiString(Column->FieldName), Order
? "ASC" : "DESC")));

Dataset->Sort = Str;
}
//---------------------------------------------------------------------------



Kind Regards,
sasan.
Larry Griffiths
2008-07-26 21:47:24 UTC
Permalink
Save the current row before ths sort and then set it back after the sort.
We do the same thing.

Larry Griffiths
Post by Sasan
Hello,
If all columns can not seen in grid and user click column that scrolled to
see it, grid sort by that filed also
columns scrolled to first column !
How can prevent from scrolling after sorting ?
/////////////////
// CODE
//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
bool Order;
AnsiString Str;
TCustomADODataSet * Dataset;
Dataset = dynamic_cast <TCustomADODataSet
*>(Column->Grid->DataSource->DataSet);
Order = Pos(" DESC", Dataset->Sort) == 0 ? true : false;
Order = Pos(Column->Field->FieldName, Dataset->Sort) == 1 ? !Order : Order;
Str = ::Format("%s %s", ARRAYOFCONST((AnsiString(Column->FieldName), Order
? "ASC" : "DESC")));
Dataset->Sort = Str;
}
//---------------------------------------------------------------------------
Kind Regards,
sasan.
Larry Griffiths
2008-07-26 21:48:22 UTC
Permalink
Save the current row Position I mean.
Sasan
2008-07-27 05:31:36 UTC
Permalink
Thank you,

But I need also save column position in visible status, After sorting
Horizontal scrollbar move to begin position (first column),
I want scrollbar stay on same position in fact sorted column stay visible !
Post by Larry Griffiths
Save the current row before ths sort and then set it back after the sort.
We do the same thing.
Larry Griffiths
Post by Sasan
Hello,
If all columns can not seen in grid and user click column that scrolled
to see it, grid sort by that filed also
columns scrolled to first column !
How can prevent from scrolling after sorting ?
/////////////////
// CODE
//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
bool Order;
AnsiString Str;
TCustomADODataSet * Dataset;
Dataset = dynamic_cast <TCustomADODataSet
*>(Column->Grid->DataSource->DataSet);
Order = Pos(" DESC", Dataset->Sort) == 0 ? true : false;
Order = Pos(Column->Field->FieldName, Dataset->Sort) == 1 ? !Order : Order;
Str = ::Format("%s %s", ARRAYOFCONST((AnsiString(Column->FieldName),
Order ? "ASC" : "DESC")));
Dataset->Sort = Str;
}
//---------------------------------------------------------------------------
Kind Regards,
sasan.
Larry Griffiths
2008-07-27 16:38:13 UTC
Permalink
I would think that you would want to save the COL then and set COL back to
the saved value after the sort.

Larry
Post by Sasan
Thank you,
But I need also save column position in visible status, After sorting
Horizontal scrollbar move to begin position (first column),
I want scrollbar stay on same position in fact sorted column stay visible !
Post by Larry Griffiths
Save the current row before ths sort and then set it back after the sort.
We do the same thing.
Larry Griffiths
Post by Sasan
Hello,
If all columns can not seen in grid and user click column that scrolled
to see it, grid sort by that filed also
columns scrolled to first column !
How can prevent from scrolling after sorting ?
/////////////////
// CODE
//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
bool Order;
AnsiString Str;
TCustomADODataSet * Dataset;
Dataset = dynamic_cast <TCustomADODataSet
*>(Column->Grid->DataSource->DataSet);
Order = Pos(" DESC", Dataset->Sort) == 0 ? true : false;
Order = Pos(Column->Field->FieldName, Dataset->Sort) == 1 ? !Order : Order;
Str = ::Format("%s %s", ARRAYOFCONST((AnsiString(Column->FieldName),
Order ? "ASC" : "DESC")));
Dataset->Sort = Str;
}
//---------------------------------------------------------------------------
Kind Regards,
sasan.
Sasan
2008-07-27 08:27:40 UTC
Permalink
I add this code for focus on selected column but not work:

DBGrid1->SelectedIndex = Column->Index; // no effect
Post by Larry Griffiths
Save the current row before ths sort and then set it back after the sort.
We do the same thing.
Larry Griffiths
Post by Sasan
Hello,
If all columns can not seen in grid and user click column that scrolled
to see it, grid sort by that filed also
columns scrolled to first column !
How can prevent from scrolling after sorting ?
/////////////////
// CODE
//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
bool Order;
AnsiString Str;
TCustomADODataSet * Dataset;
Dataset = dynamic_cast <TCustomADODataSet
*>(Column->Grid->DataSource->DataSet);
Order = Pos(" DESC", Dataset->Sort) == 0 ? true : false;
Order = Pos(Column->Field->FieldName, Dataset->Sort) == 1 ? !Order : Order;
Str = ::Format("%s %s", ARRAYOFCONST((AnsiString(Column->FieldName),
Order ? "ASC" : "DESC")));
Dataset->Sort = Str;
}
//---------------------------------------------------------------------------
Kind Regards,
sasan.
Loading...