//دع عرض العمود في الـ TDBgrid يتغير بشكل أوتوماتيكي ..
(* هدا الكود عرض الأعمدة بحيث تناسب محتوياتها أيضاً ميزة أخرى هدا السطر من الكود سيخيرك فيما إدا أردت التأثير فقط على العمود الأخير أم على الكل If aChangeAll = False then only the last col will be modified. *) procedure ChangeColumnWidths(DBGrid: TDBGrid; aChangeAll: boolean); var NewWidth, TotalColumnWidth, ColumnCount, GridClientWidth, StepWidth, i: integer; begin ColumnCount := DBGrid.Columns.Count; if ColumnCount = 0 then Exit; //البحث عن العرض الحقيقي لجميع الأعمدة TotalColumnWidth := 0; for i := 0 to ColumnCount-1 do TotalColumnWidth := TotalColumnWidth + DBGrid.Columns.Width; if dgColLines in DBGrid.Options then TotalColumnWidth := TotalColumnWidth + ColumnCount; //ما هو العرض الكلي للـ أقصد لمجموع عرض الأعمدة في الـ Grid ? GridClientWidth := DBGrid.Width - GetSystemMetrics(SM_CXVSCROLL); if dgIndicator in DBGrid.Options then begin GridClientWidth := GridClientWidth - IndicatorWidth; if dgColLines in DBGrid.Options then Dec(GridClientWidth); end; if DBGrid.BorderStyle = bsSingle then begin if DBGrid.Ctl3D then // 2 * 2 Pixel GridClientWidth := GridClientWidth - 4 else // 2 * 1 Pixel GridClientWidth := GridClientWidth - 2; end; // توسيع عرض الأعمدة if TotalColumnWidth < GridClientWidth then begin StepWidth := (GridClientWidth - TotalColumnWidth) div ColumnCount; // جميع الأعمدة if aChangeAll then begin for i := 0 to ColumnCount-1 do DBGrid.Columns.Width := DBGrid.Columns.Width + StepWidth; // فقط الأخير end else DBGrid.Columns[ColumnCount-1].Width := DBGrid.Columns[ColumnCount-1].Width + ColumnCount * StepWidth; end // تقليل عرض الأعمدة else if TotalColumnWidth > GridClientWidth then begin StepWidth := (TotalColumnWidth - GridClientWidth) div ColumnCount; if (TotalColumnWidth - GridClientWidth) mod ColumnCount <> 0 then Inc(StepWidth); // جميع الأعمدة if aChangeAll then begin for i := 0 to ColumnCount-1 do begin NewWidth := DBGrid.Columns.Width - StepWidth; if (NewWidth > 5) then DBGrid.Columns.Width := NewWidth; end; // فقط الأخير end else begin NewWidth := DBGrid.Columns[ColumnCount-1].Width - ColumnCount * StepWidth; if (NewWidth > 5) then DBGrid.Columns[ColumnCount-1].Width := NewWidth; end; end; end;
المبرمج عماد .... B) B)