Bla*_*tad 3 c# datagridview winforms
我已经实现了一个使用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");
}
}
Run Code Online (Sandbox Code Playgroud)
更换
TextRenderer.DrawText(e.Graphics, e.Value.ToString(),
e.CellStyle.Font, e.CellBounds,
e.CellStyle.ForeColor, TextFormatFlags.VerticalCenter);
Run Code Online (Sandbox Code Playgroud)
同
e.PaintContent(e.ClipBounds);
Run Code Online (Sandbox Code Playgroud)
正确地显示它,当然我希望能够自定义内容的绘画.我也试过用
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds);
Run Code Online (Sandbox Code Playgroud)

但它并没有像它那样画出来
e.Paint(e.ClipBounds, e.PaintParts);
Run Code Online (Sandbox Code Playgroud)
当我正在绘制一个不需要我自定义绘画的单元格时,我在实际代码中使用了e.Paint.
如何使e.Graphics.DrawString看起来与e.Paint相同或获取TextRenderer.DrawText以正确显示&符号?
您想使用TextRenderer版本,因为DrawString实际上只能用于打印:
TextRenderer.DrawText(e.Graphics, e.Value.ToString(),
e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor,
TextFormatFlags.NoPrefix | TextFormatFlags.VerticalCenter);
Run Code Online (Sandbox Code Playgroud)
NoPrefix标志将正确显示&符号.
| 归档时间: |
|
| 查看次数: |
702 次 |
| 最近记录: |