1

How to resize cell content (font size) if it doesn't fit. I don't want word-wrap or change cell's size since the form has to fit on single page. The text might have various lengths. It might contain spaces but doesn't have to.

@edit

I've made a mistake. The control I meant is not a XtraGrid but XRTable.

user2475983
  • 1,916
  • 2
  • 13
  • 20
  • 2
    What have you already tried? – Zach Ross-Clyne Jan 19 '16 at 11:23
  • Reading their documentation, couldnt really find anything usefull. I can probably calculate the text height and width by hand. However, I'm asking if there's some kind of way foreseen by devexpress itself. – user2475983 Jan 19 '16 at 11:41
  • I'm assuming you're using System Drawing somewhere? If so, try using this https://msdn.microsoft.com/en-us/library/6xe5hazb%28v=vs.110%29.aspx and looping through font sizes to find the one that works best for your area? – Zach Ross-Clyne Jan 19 '16 at 12:01
  • Did you find a solution? – Majkl Jan 20 '16 at 10:52
  • @Majkl Your solution might work, I didn't have time to check it yet, gonna update immediately when I do so. – user2475983 Jan 20 '16 at 12:15

2 Answers2

1

I suggest you to look at the Appearances at first. There are multiple ways to customize appearances of individual rows and cells.

If these options are not helpful, you can manually draw cell content as you needed using the Custom Drawing feature. For example, you can use the GridView.CustomDrawCell event to check whether or not the cell's content is exceed the cell's bounds and update the font of this cell accordingly.

Related example: How to: Custom Draw Cells Depending Upon Cell Values

DmitryG
  • 17,677
  • 1
  • 30
  • 53
0

You can change the font of the cell where the text flows as follows

private void gvView_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
    {
      if (e.Column != null && e.Column.Name == bgcStav.Name)
      {
        float minFontSize = 6;
        string text = "teeeeeeeeeeeeeext";
        int minWidth = gvView.CalcColumnBestWidth(bgcStav);        
        SizeF s = e.Appearance.CalcTextSize(e.Graphics, text, minWidth);
        if (s.Width >= minWidth)
        {
          e.Appearance.Font = new Font(e.Appearance.Font.FontFamily, minFontSize);          
        }
      }
    }

but it is much better to trim the text if it overflows (you do not know how long the text can be), when you do not want to use wordwrap

 private void gvView_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
    {
      if (e.Column != null && e.Column.Name == bgcStav.Name)
      {
        string text = e.DisplayText;
        string newText = "";
        int maxWidth = e.Bounds.Width - 20;
        SizeF textSize =e.Graphics.MeasureString(text, e.Appearance.Font);
        if (textSize.Width >= maxWidth)
        {
          string textPom = "";
          for (int i = 0; i < text.Length; i++)
          {
            textPom = text.Substring(0, i) + "...";
            textSize = e.Graphics.MeasureString(textPom, e.Appearance.Font);
            if (textSize.Width >= maxWidth)
            {
              newText = text.Substring(0, i - 1) + "...";
              break;
            }
          }
          e.DisplayText = newText;
        }           
      }
    }

Advantage of this solution is that the crop only what is displaied, but in the datatable text remains in its original form

Majkl
  • 765
  • 1
  • 9
  • 25
  • Sorry for late reply, Your solution would work on `XtraGrid`, but I've made mistake in my question. Please see the update. – user2475983 Jan 25 '16 at 16:24