C# winform 宽度不够时如何设置隐藏方向?

问题描述 投票:0回答:1

我在 C#/.NET 6.0/WinForms 中使用 DataGridView。 有没有办法在列不够宽的情况下设置文字隐藏的方向? 当列宽比文本短时,我想从头(左)开始隐藏文本。 例如: 对于“ABCDEFG”,我想显示“...DEFG”而不是“ABCD...”。 我已将文本对齐方式设置为中右,但不起作用。

只有添加CellPainting事件来计算文本并覆盖原始文本吗?

c# winforms controls
1个回答
0
投票

CellFormatting”事件处理程序可用于从左侧修剪网格单元格内的文本。

如下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        FillDataGridView();
        SetDataGridViewColumnStyle();
    }
    private void FillDataGridView()
    {
        var data = new System.Data.DataTable();
        data.Columns.Add("Col1", typeof(string));
        data.Columns.Add("Col2", typeof(string));
        data.Columns.Add("Col3", typeof(string));
        data.Rows.Add(new object[] { "ABCDEFG", "ABCDEFG", "ABCDEFG" });
        data.Rows.Add(new object[] { "ABCDEFG", "ABCDEFG", "ABCDEFG" });
        data.Rows.Add(new object[] { "ABCDEFG", "ABCDEFG", "ABCDEFG" });
        data.Rows.Add(new object[] { "ABCDEFG", "ABCDEFG", "ABCDEFG" });
        dataGridView1.Columns.Clear();
        dataGridView1.DataSource = data;
    }
    private void SetDataGridViewColumnStyle()
    {
        dataGridView1.Columns[0].Width = 50;
        dataGridView1.Columns[1].Width = 40;
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {
            column.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            column.DefaultCellStyle.Padding = new Padding(0);
        }
    }
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value is string @ValueText)
        {
            e.Value = GetLeftSideTrimmedText(e.ColumnIndex, ValueText, (Control)sender);
            e.FormattingApplied = true;
        }
    }
    private string GetLeftSideTrimmedText(int ColumnIndex, string ValueText, Control TheControl, int TextGap = 5)
    {
        const string ThreeDots = "...";
        ValueText = ValueText.Trim();

        var WidthOfText = GetTextWidth(ValueText, TheControl);
        var WidthOfThreeDots = GetTextWidth(ThreeDots, TheControl);
        int WidthOfColumn = dataGridView1.Columns[ColumnIndex].Width;
        if (WidthOfText < WidthOfColumn)
            return ValueText;
        else
        {
            var LeftSideTrimmedText = "";
            var ListOfCharacters = ValueText.ToList();
            ListOfCharacters.Reverse();
            var ListOfReverseCharacters = ListOfCharacters;
            var LastCharacter = ListOfReverseCharacters.First();
            var ListOfReverseCharacterWidths = ListOfReverseCharacters
                .Select(ch => GetTextWidth(ch.ToString(), TheControl))
                .ToList();
            int WidthOfLastCharacter = ListOfReverseCharacterWidths.First();
            if (WidthOfLastCharacter + WidthOfThreeDots > WidthOfColumn)
                return LastCharacter.ToString();
            int CalculatedTextWidth = 0;
            for (int i = 0; i < ListOfReverseCharacters.Count; i++)
            {
                CalculatedTextWidth += ListOfReverseCharacterWidths[i];
                if (CalculatedTextWidth < WidthOfColumn)
                    LeftSideTrimmedText = ListOfReverseCharacters[i] + LeftSideTrimmedText;
                else
                    return ThreeDots + LeftSideTrimmedText;
            }
        }
        return ValueText;
    }
    private static readonly Graphics _graphics = Graphics.FromImage(new Bitmap(1, 1));
    private static int GetTextWidth(string Text, Control TheControl)
    {
        var GridFontWithPointGraphicUnit = GetFontWithNewGraphicUnit(TheControl.Font, GraphicsUnit.Point);
        return (int)_graphics.MeasureString(Text, GridFontWithPointGraphicUnit).Width;
    }
    private static Font GetFontWithNewGraphicUnit(Font BaseFont, GraphicsUnit NewGraphicsUnit)
    {
        return new Font(BaseFont.FontFamily, BaseFont.Size, BaseFont.Style, NewGraphicsUnit);
    }
}

程序输出如下:

Output of program

© www.soinside.com 2019 - 2024. All rights reserved.