datagridview 一个单元格显示文字和图片

这篇博客介绍了如何在DataGridView控件的一个单元格中同时显示文字和图片,并且当文字内容超出时自动显示省略号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值