datagridview 一个单元格显示文字和图片
文字超出时显示...
private void dgv_Test_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{/**/
if (e.ColumnIndex >= 0 && e.RowIndex < strings.Count && e.RowIndex >= 0)
{
//绘制背景色
e.PaintBackground(e.ClipBounds, true);
StringBuilder sb = new StringBuilder(e.Value.ToString());
var textSize = Size.Empty;
while (true)
{
textSize = e.Graphics.MeasureString(sb.ToString(), dgv_Test.DefaultCellStyle.Font).ToSize();
if (e.CellBounds.Left + textSize.Width + 18 < e.CellBounds.Right)
{
var locationText = new Point(e.CellBounds.Location.X, e.CellBounds.Location.Y + e.CellBounds.Height / 2 - (textSize.Height / 2));
e.Graphics.DrawString(sb.ToString(), dgv_Test.DefaultCellStyle.Font, Brushes.Black, locationText);
e.Graphics.DrawImage(Properties.Resources.Mark_16, e.CellBounds.Left + textSize.Width, e.CellBounds.Top + 4, 16, 16);
break;
}
else
{
sb = sb.Remove(sb.Length - 4, 4).Append("...");
continue;
}
}
}
//提交处理
e.Handled = true;/**/
}