如果文本不适合单元格(XRTable),请调整字体大小

时间:2016-01-19 11:22:42

标签: c# devexpress devexpress-windows-ui

如果不适合,请如何调整单元格内容(字体大小)。我不想要自动换行或更改单元格的大小,因为表单必须适合单页。文本可能有不同的长度。它可能包含空格但不必包含空格。

@edit

我犯了一个错误。我的意思不是XtraGrid而是XRTable

2 个答案:

答案 0 :(得分:1)

我建议您先查看Appearancescustomize appearances of individual rows and cells有多种方法。

如果这些选项无效,您可以使用Custom Drawing功能手动绘制单元格内容。例如,您可以使用GridView.CustomDrawCell事件来检查单元格的内容是否超出单元格的范围,并相应地更新此单元格的字体。

相关示例:How to: Custom Draw Cells Depending Upon Cell Values

答案 1 :(得分:0)

您可以更改文本流动的单元格的字体,如下所示

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);          
        }
      }
    }

但如果文本溢出(你不知道文本可以有多长时间),当你不想使用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;
        }           
      }
    }

这个解决方案的优势在于裁剪只会被置换,但在数据表文本中仍保持其原始形式

相关问题