确保文本在 dataGridView 列中换行 [英] Ensuring text wraps in a dataGridView column

查看:27
本文介绍了确保文本在 dataGridView 列中换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有特定列的 dataGridView.当我在 dataGridView 中写入长文本时,它向我显示了一个带有省略号的缩短版本,因为该列不够宽,无法显示整个字符串.

I have dataGridView with a particular column. When I write long text in dataGridView it shows me a shortened version, with ellipses, because the column isn't wide enough to display the entire string.

| textdsadasda...  |

如果我想让 dataGridView 在下一行显示此文本,或者将文本换行,我必须做什么?

What do I must to do if I want to dataGridView show this text in next line, or wrap the text?

| textdsadasda     |
| dasdasa          |  (continuation of line above)

如何做到这一点?

推荐答案

可能正在处理单元格绘制事件可以帮到你

May be handling cell painting event can help you

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.Value == null)
        return;
    var s = e.Graphics.MeasureString(e.Value.ToString(), dataGridView1.Font);
    if (s.Width > dataGridView1.Columns[e.ColumnIndex].Width)
    {
        using (
  Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
  backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
            e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, e.CellBounds,StringFormat.GenericDefault);
            dataGridView1.Rows[e.RowIndex].Height = (int)(s.Height * Math.Ceiling( s.Width / dataGridView1.Columns[e.ColumnIndex].Width)) ;
            e.Handled = true;
        }
    }
}

这篇关于确保文本在 dataGridView 列中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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