2013-07-07 12 views
5

मेरे पास एक टीडीबीजीड है। यह काम करता है, लेकिन दिखाए गए कॉलम बहुत बड़े हैं।कॉलम चौड़ाई समायोजित करें डीबीजीड

मैं "ऑटो-फ़िक्स कॉलम चौड़ाई" कैसे सेट कर सकता हूं?

+4

प्रत्येक कॉलम की चौड़ाई संबंधित फ़ील्ड के घोषित आकार में समायोजित प्रति डिफ़ॉल्ट है। यदि यह वास्तविकता से मेल नहीं खाता है तो आपको अपने डेटाबेस डिज़ाइन पर पुनर्विचार करना चाहिए। –

+0

@Uwe, यह '0' वर्णों की फ़ील्ड की 'डिस्प्लेविड्थ' गिनती के लिए सच है, जो ताहोमा आकार 8 फ़ॉन्ट (डिफ़ॉल्ट स्केलिंग पर) के साथ प्रत्येक 6px चौड़ा है। यदि आप प्रदर्शित कर रहे थे, तो मान लें कि 100 'डब्ल्यू' वर्ण (जो प्रत्येक 10px चौड़ा है) कहें, डिफ़ॉल्ट कॉलम चौड़ाई वास्तव में आवश्यकतानुसार 400px संकुचित होगी (जब' डिस्प्लेविड्थ 100 होगी)। तो चौड़ाई को स्वत: समायोजित करने के लिए एक विपरीत कारण भी हो सकता है। – TLama

उत्तर

10

आवश्यक कॉलमविड्थ ग्रिड कैनवास की सेटिंग्स और प्रत्येक फ़ील्ड के प्रदर्शन टेक्स्ट की अधिकतम लंबाई से निर्भर है।

procedure FitGrid(Grid: TDBGrid); 
const 
    C_Add=3; 
var 
    ds: TDataSet; 
    bm: TBookmark; 
    i: Integer; 
    w: Integer; 
    a: Array of Integer; 
begin 
    ds := Grid.DataSource.DataSet; 
    if Assigned(ds) then 
    begin 
    ds.DisableControls; 
    bm := ds.GetBookmark; 
    try 
     ds.First; 
     SetLength(a, Grid.Columns.Count); 
     while not ds.Eof do 
     begin 
     for I := 0 to Grid.Columns.Count - 1 do 
     begin 
      if Assigned(Grid.Columns[i].Field) then 
      begin 
      w := Grid.Canvas.TextWidth(ds.FieldByName(Grid.Columns[i].Field.FieldName).DisplayText); 
      if a[i] < w then 
       a[i] := w ; 
      end; 
     end; 
     ds.Next; 
     end; 
     for I := 0 to Grid.Columns.Count - 1 do 
     Grid.Columns[i].Width := a[i] + C_Add; 
     ds.GotoBookmark(bm); 
    finally 
     ds.FreeBookmark(bm); 
     ds.EnableControls; 
    end; 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    FitGrid(DBgrid1) 
end; 
+5

शून्यमोमारी की आवश्यकता नहीं है। SetLength शून्य के साथ सभी नए सदस्यों को शुरू करता है। –

+0

अच्छा समाधान। बस एक नाइट पिक: अंदरूनी 'अगर एक [i] lurker

+0

जो एक बड़े डेटा सेट पर लंबे समय तक ले सकता है, और जब आपके पास बहुत लंबी सामग्री के साथ भटकने वाला रिकॉर्ड होता है। जो आकस्मिक कारण हैं कि 'टीडीबीजीड्रिड' में पहली बार इस प्रकार की कार्यक्षमता नहीं है, और विंडोज एक्सप्लोरर कुछ परिस्थितियों में 'ठीक से' आकार बदलता नहीं है। –

3

bummi के जवाब के मामूली संशोधन का बीमा करने कि शीर्षक पंक्ति (पंक्ति 0) छोटा नहीं है

procedure FitGrid(Grid: TDBGrid); 
const 
    C_Add=3; 
var 
    ds: TDataSet; 
    bm: TBookmark; 
    i: Integer; 
    w: Integer; 
    a: Array of Integer; 
begin 
    ds := Grid.DataSource.DataSet; 
    if Assigned(ds) then 
    begin 
    ds.DisableControls; 
    bm := ds.GetBookmark; 
    try 
     ds.First; 
     SetLength(a, Grid.Columns.Count); 
     while not ds.Eof do 
     begin 
     for I := 0 to Grid.Columns.Count - 1 do 
     begin 
      if Assigned(Grid.Columns[i].Field) then 
      begin 
      w := Grid.Canvas.TextWidth(ds.FieldByName(Grid.Columns[i].Field.FieldName.).DisplayText); 
      if a[i] < w then 
       a[i] := w ; 
      end; 
     end; 
     ds.Next; 
     end; 
     //if fieldwidth is smaller than Row 0 (field names) fix 
     for I := 0 to Grid.Columns.Count - 1 do 
     begin 
     w := Grid.Canvas.TextWidth(Grid.Columns[i].Field.FieldName); 
     if a[i] < w then 
      a[i] := w ; 
     end; 

     for I := 0 to Grid.Columns.Count - 1 do 
     Grid.Columns[i].Width := a[i] + C_Add; 
     ds.GotoBookmark(bm); 
    finally 
     ds.FreeBookmark(bm); 
     ds.EnableControls; 
    end; 
    end; 
end; 
+0

हाय, मैं डेल्फी के लिए काफी नया हूं और मैं आपको यह बताने के लिए कहूंगा कि यह विस्तार से कैसे काम कर रहा है। मैं, डब्ल्यू और एक अच्छा, ect जैसे चर क्या हैं। मैं वास्तव में आभारी रहूंगा। – user2886091

2

bummi के जवाब के मामूली संशोधन, बीमा करने के लिए कि शीर्षक पंक्ति (पंक्ति 0) छोटा कर दिया नहीं है और अतिरिक्त अंतरिक्ष बीमा करने के लिए कि शीर्षक पंक्ति (पंक्ति 0) छोटा कर दिया नहीं है

procedure FitGrid(Grid: TDBGrid); 
const 
    C_Add = 3; 
var 
    ds: TDataSet; 
    bm: TBookmark; 
    i: Integer; 
    w: Integer; 
    a: array of Integer; 
begin 
    ds := Grid.DataSource.DataSet; 

    if not Assigned(ds) then 
    exit; 

    if Grid.Columns.Count = 0 then 
    exit; 

    ds.DisableControls; 
    bm := ds.GetBookmark; 
    try 
    ds.First; 
    SetLength(a, Grid.Columns.Count); 
    for i := 0 to Grid.Columns.Count - 1 do 
     if Assigned(Grid.Columns[i].Field) then 
     a[i] := Grid.Canvas.TextWidth(Grid.Columns[i].FieldName); 

    while not ds.Eof do 
    begin 

     for i := 0 to Grid.Columns.Count - 1 do 
     begin 
     if not Assigned(Grid.Columns[i].Field) then 
      continue; 

     w := Grid.Canvas.TextWidth(ds.FieldByName(Grid.Columns[i].Field.FieldName).DisplayText); 

     if a[i] < w then 
      a[i] := w; 
     end; 
     ds.Next; 
    end; 

    w := 0; 
    for i := 0 to Grid.Columns.Count - 1 do 
    begin 
     Grid.Columns[i].Width := a[i] + C_Add; 
     inc(w, a[i] + C_Add); 
    end; 

    w := (Grid.ClientWidth - w - 20) div (Grid.Columns.Count); 

    if w > 0 then 
     for i := 0 to Grid.Columns.Count - 1 do 
     Grid.Columns[i].Width := Grid.Columns[i].Width + w; 


    ds.GotoBookmark(bm); 
    finally 
    ds.FreeBookmark(bm); 
    ds.EnableControls; 
    end; 
end; 
0

bummi की, TheSteven की और जेन्स के उत्तरों के मामूली संशोधन के प्रत्येक स्तंभ पर आवंटित किया जाएगा, और अतिरिक्त अंतरिक्ष allo हो जाएगा प्रत्येक कॉलम पर cated, और खाते में कॉलम की दृश्यता लें।

procedure FitGrid(const Grid: TDBGrid; const CoverWhiteSpace: Boolean = True); 
const 
    C_Add=3; 
var 
    DS: TDataSet; 
    BM: TBookmark; 
    I, W, VisibleColumnsCount: Integer; 
    A: array of Integer; 
    VisibleColumns: array of TColumn; 
begin 
    DS := Grid.DataSource.DataSet; 
    if Assigned(DS) then 
    begin 
    VisibleColumnsCount := 0; 
    SetLength(VisibleColumns, Grid.Columns.Count); 
    for I := 0 to Grid.Columns.Count - 1 do 
     if Assigned(Grid.Columns[I].Field) and (Grid.Columns[I].Visible) then 
     begin 
     VisibleColumns[VisibleColumnsCount] := Grid.Columns[I]; 
     Inc(VisibleColumnsCount); 
     end; 
    SetLength(VisibleColumns, VisibleColumnsCount); 

    DS.DisableControls; 
    BM := DS.GetBookmark; 
    try 
     DS.First; 
     SetLength(A, VisibleColumnsCount); 
     while not DS.Eof do 
     begin 
     for I := 0 to VisibleColumnsCount - 1 do 
     begin 
      W := Grid.Canvas.TextWidth(DS.FieldByName(VisibleColumns[I].Field.FieldName).DisplayText); 
      if A[I] < W then 
       A[I] := W; 
     end; 
     DS.Next; 
     end; 
     //if fieldwidth is smaller than Row 0 (field names) fix 
     for I := 0 to VisibleColumnsCount - 1 do 
     begin 
     W := Grid.Canvas.TextWidth(VisibleColumns[I].Field.FieldName); 
     if A[I] < W then 
      A[I] := W; 
     end; 

     W := 0; 
     if CoverWhiteSpace then 
     begin 
     for I := 0 to VisibleColumnsCount - 1 do 
      Inc(W, A[I] + C_Add); 
     W := (Grid.ClientWidth - W - 20) div VisibleColumnsCount; 
     if W < 0 then 
      W := 0; 
     end; 

     for I := 0 to VisibleColumnsCount - 1 do 
     VisibleColumns[I].Width := A[I] + C_Add + W; 
     DS.GotoBookmark(BM); 
    finally 
     DS.FreeBookmark(BM); 
     DS.EnableControls; 
    end; 
    end; 
end; 
संबंधित मुद्दे