C#的DataGridView:超长文字与&QUOT截断; ..."左侧当列右对齐 [英] C# DataGridView: Long Text Truncated with "..." on the Left Side When the Column is Right-Aligned
问题描述
我有一个关于小区截断问题(改为...):
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...
我想我已经说清楚了。
I think I've made myself clear.
感谢。请帮助我。
彼得
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:超长文字与&QUOT截断; ..."左侧当列右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!