C# DataGridView:用“..."截断的长文本当列右对齐时在左侧 [英] C# DataGridView: Long Text Truncated with "..." on the Left Side When the Column is Right-Aligned

查看:25
本文介绍了C# DataGridView:用“..."截断的长文本当列右对齐时在左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于单元格截断的问题(替换为..."):

I have a question about the cell truncation (replaced with "..."):

当列右对齐时如何在单元格左侧显示替换..."?

How to display the replacement "..." on the left side of a cell when the column is right-aligned?

我使用的是非等宽字体,所以我不能仅仅通过计算字符来做一些字符串操作作为一种解决方法,我需要一个解决方案.我相信应该有.

I'm using non-equal-width font, so I cannot just count the characters to do some string manipulation as a workaround, I need a solution. I believe there should be.

为了说明我的问题,我在这里模拟我的 DataGridView

To illustrate my question, I'm simulating my DataGridView here

Left Context (Right aligned column)        | Center Word | Right Context (Left aligned column)
                left context not truncated | CenterWord  | Right context not truncated
...Here is the long left context truncated | CenterWord  | Here is the long right context truncated...

我想我已经说清楚了.

谢谢.请帮帮我.

彼得

P.S.:同样的问题可以在这个链接中找到:http://objectmix.com/csharp/341736-datagridview-cell-format-question.html

P.S.: the same question can be found at this link: http://objectmix.com/csharp/341736-datagridview-cell-format-question.html

推荐答案

这绝对是一件不寻常的事情 - 但(就像其他任何事情一样)它可以做到.这是测量字符串大小并将其与单元格大小进行比较的问题.(请注意,我假设数据是由用户输入的.如果您是数据绑定,则基本上必须使用其他事件.)

It's definitely an unusual thing to do - but (like anything else) it can be done. It's a question of measuring the size of the string and comparing it with the size of the cell. (Note that I assume that the data is entered by a user. If you're databinding you basically have to consume other events.)

这可行,但可能需要一些微调:

This works but might need some fine tuning:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        dataGridView1.Columns.Add("col1", "col1");
        dataGridView1.Columns[0].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        dataGridView1.Columns.Add("col2", "col2");
        dataGridView1.Columns.Add("col3", "col3");

        dataGridView1.Rows.Add();
        dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
        dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);              
    }

    void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
    {
        if (e.Column.Index == 0)
        {
            // We need to truncate everything again when the width changes
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                RightTruncateText(row.Cells[0]);
            }
        }
    }

    void RightTruncateText(DataGridViewCell cell)
    {                        
        // check if the content is too long:
        using (Graphics g = Graphics.FromHwnd(this.Handle))
        {
            SizeF size = g.MeasureString((string)cell.Tag, dataGridView1.Font); // NOTE: using the tag

            if (size.Width > cell.Size.Width)
            {
                StringBuilder truncated = new StringBuilder((string)cell.Tag);

                truncated.Insert(0, "...");

                // Truncate the string until small enough (NOTE: not optimized in any way!)                        
                while (size.Width > cell.Size.Width)
                {
                    truncated.Remove(3, 1);
                    size = g.MeasureString(truncated.ToString(), dataGridView1.Font);
                }
                cell.Value = truncated.ToString();
            }
            else
            {
                cell.Value = cell.Tag;
            }
        }
    }

    void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            // Save the value in the tag but show the truncated value
            DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.Tag = cell.Value; // Saving the actual state
            RightTruncateText(cell);
        }
    }
}

这篇关于C# DataGridView:用“..."截断的长文本当列右对齐时在左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆