PDA

View Full Version : سورت کردن و چیدن ستونهای یک DbGrid



hedayat
شنبه 14 آبان 1384, 17:12 عصر
سورت کردن و چیدن ستونهای یک DbGrid با کلیک کردن بر روی سر تیتر ستونها :

ابتدا در رویدادِ DBGridMouseMove بنویسید :

procedure TForm1.DBGrid1MouseMove
(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
pt: TGridcoord;
begin
pt:= DBGrid1.MouseCoord(x, y);

if pt.y=0 then
DBGrid1.Cursor:=crHandPoint
else
DBGrid1.Cursor:=crDefault;
End;

سپس در رویداد DBGridTitleClick بنویسید :

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
{$J+}
const PreviousColumnIndex : integer = 0;
{$J-}
begin
if DBGrid1.DataSource.DataSet is TCustomADODataSet then
with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
begin
try
DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
except
end;

Column.title.Font.Style :=
Column.title.Font.Style + [fsBold];
PreviousColumnIndex := Column.Index;

if (Pos(Column.Field.FieldName, Sort) = 1)
and (Pos(' DESC', Sort)= 0) then
Sort := Column.Field.FieldName + ' DESC'
else
Sort := Column.Field.FieldName + ' ASC';
end;
end;

oghab
سه شنبه 17 آبان 1384, 09:45 صبح
سلام
حیف همچین کدی است که از راست به چپ نمایش پیدا کنه. پس من چپ به راستش می کنم:


سورت کردن و چیدن ستونهای یک DbGrid با کلیک کردن بر روی سر تیتر ستونها :

ابتدا در رویدادِ DBGridMouseMove بنویسید :



procedure TForm1.DBGrid1MouseMove
(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
pt: TGridcoord;
begin
pt:= DBGrid1.MouseCoord(x, y);

if pt.y=0 then
DBGrid1.Cursor:=crHandPoint
else
DBGrid1.Cursor:=crDefault;
End;


سپس در رویداد DBGridTitleClick بنویسید :



procedure TForm1.DBGrid1TitleClick(Column: TColumn);
{$J+}
const PreviousColumnIndex : integer = 0;
{$J-}
begin
if DBGrid1.DataSource.DataSet is TCustomADODataSet then
with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
begin
try
DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
except
end;

Column.title.Font.Style :=
Column.title.Font.Style + [fsBold];
PreviousColumnIndex := Column.Index;

if (Pos(Column.Field.FieldName, Sort) = 1)
and (Pos(' DESC', Sort)= 0) then
Sort := Column.Field.FieldName + ' DESC'
else
Sort := Column.Field.FieldName + ' ASC';
end;
end;