我有一个关于单元格截断的问题(替换为"..."):
如何显示替换"...“当列右对齐时,在单元格的左侧?
我使用的是非等宽字体,所以我不能只计算字符来做一些字符串操作作为一种解决办法,我需要一个解决方案。我相信应该有。
为了说明我的问题,我在这里模拟我的DataGridView
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...我想我已经说得很清楚了。
谢谢。请帮帮我。
彼得
附言:同样的问题可以在这个链接上找到:http://objectmix.com/csharp/341736-datagridview-cell-format-question.html
发布于 2010-10-11 10:17:30
这绝对是一件不同寻常的事情--但(和其他事情一样)它是可以做到的。问题是测量字符串的大小,并将其与单元格的大小进行比较。(注意,我假设数据是由用户输入的。如果要进行数据绑定,基本上必须使用其他事件。)
这是可行的,但可能需要进行一些微调:
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);
}
}
}发布于 2010-10-11 14:47:11
我已经做了一个变通方法,它正在工作,除了"...“(截断效果很好)
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base.OnCellPainting(e);
if (e.RowIndex >= 0 && e.ColumnIndex >= 0 &&
CustomRightToLeftColumnNames.Contains(this.Columns[e.ColumnIndex].Name))
{
// Method 2:
e.PaintBackground(e.CellBounds, true);
if (e.FormattedValue != null)
{
TextFormatFlags flags = TextFormatFlags.RightToLeft |
TextFormatFlags.VerticalCenter |
TextFormatFlags.Right |
TextFormatFlags.LeftAndRightPadding;// |
//TextFormatFlags.EndEllipsis;
TextRenderer.DrawText
(
e.Graphics,
e.FormattedValue.ToString(),
e.CellStyle.Font,
e.CellBounds,
e.CellStyle.ForeColor,
flags
);
}
e.Handled = true;
}
}这个解决方案的唯一问题是,我不知道如何设置TextFormatFlags来获得我想要的正确行为,就像DataGridView.RightToLeft = Yes一样。
如果我打开TextFormatFlags.EndEllipsis,三个点"...“将出现在左侧,但它从字符串的右端截断。
我不确定打开TextFormatFlags枚举的哪个标志。
https://stackoverflow.com/questions/3902341
复制相似问题