Ensuring text wraps in a dataGridView column

前端 未结 9 1558
感动是毒
感动是毒 2021-01-01 17:41

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

9条回答
  •  长发绾君心
    2021-01-01 18:09

    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;
            }
        }
    }
    

提交回复
热议问题