【发布时间】:2016-11-22 23:17:18
【问题描述】:
我已经实现了一个使用 TextRenderer.DrawText 的 CellPainting 事件处理程序,它一直运行良好,直到一个单元格中有一个 & 符号。单元格在编辑单元格时正确显示与符号,但在完成编辑并绘制时,它显示为一条小线(不是下划线)。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace StackOverFlowFormExample {
public partial class DataGridViewImplementation : DataGridView {
public DataGridViewImplementation() {
InitializeComponent();
this.ColumnCount = 1;
this.CellPainting += DGV_CellPainting;
}
private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
if (!e.Handled && e.RowIndex > -1 && e.Value != null) {
e.PaintBackground(e.CellBounds, false);
TextRenderer.DrawText(e.Graphics, e.Value.ToString(),
e.CellStyle.Font, e.CellBounds,
e.CellStyle.ForeColor, TextFormatFlags.VerticalCenter);
e.Handled = true;
}
}
}
}
//creating the datagridview
public partial class MainForm : Form {
public MainForm() {
InitializeComponent();
DataGridViewImplementation dgvi = new DataGridViewImplementation();
this.Controls.Add(dgvi);
dgvi.Rows.Add("this is a & value");
}
}
替换
TextRenderer.DrawText(e.Graphics, e.Value.ToString(),
e.CellStyle.Font, e.CellBounds,
e.CellStyle.ForeColor, TextFormatFlags.VerticalCenter);
与
e.PaintContent(e.ClipBounds);
正确显示它,当然我希望能够自定义内容的绘画。 我也试过使用
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds);
但它的画法并不像
e.Paint(e.ClipBounds, e.PaintParts);
当绘制不需要我的自定义绘制的单元格时,我在实际代码中使用 e.Paint。
如何让 e.Graphics.DrawString 看起来与 e.Paint 相同或让 TextRenderer.DrawText 正确显示 & 符号?
【问题讨论】:
标签: c# winforms datagridview