确保文字环绕在一个DataGridView列 [英] Ensuring text wraps in a dataGridView column

查看:179
本文介绍了确保文字环绕在一个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)

如何才能做到这一点?

How can this be done?

推荐答案

可能是处理单元写生活动可以帮助你

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天全站免登陆