मुझे यहां केडी 2ND द्वारा असंतोषजनक समाधान मिला। ऐसा लगता है कि इस तरह के छोटे बदलाव के लिए सेल पेंटिंग को पूरी तरह से लागू करने के लिए मूर्खतापूर्ण लगता है - कॉलम हेडर & चयनित पंक्तियों के चित्रकला को संभालने के लिए बहुत सारे काम हैं।
// you can also handle the CellPainting event for the grid rather than
// creating a grid subclass as I have done here.
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
var isSelected = e.State.HasFlag(DataGridViewElementStates.Selected);
e.Paint(e.ClipBounds, DataGridViewPaintParts.Background
//| DataGridViewPaintParts.Border
//| DataGridViewPaintParts.ContentBackground
//| DataGridViewPaintParts.ContentForeground
| DataGridViewPaintParts.ErrorIcon
| DataGridViewPaintParts.Focus
| DataGridViewPaintParts.SelectionBackground);
using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor),
selectedForeBrush = new SolidBrush(e.CellStyle.SelectionForeColor))
{
if (e.Value != null)
{
StringFormat strFormat = new StringFormat();
strFormat.Trimming = StringTrimming.Character;
var brush = isSelected ? selectedForeBrush : foreBrush;
var fs = e.Graphics.MeasureString((string)e.Value, e.CellStyle.Font);
var topPos= e.CellBounds.Top + ((e.CellBounds.Height - fs.Height)/2);
// I found that the cell text is drawn in the wrong position
// for the first cell in the column header row, hence the 4px
// adjustment
var leftPos= e.CellBounds.X;
if (e.RowIndex == -1 && e.ColumnIndex == 0) leftPos+= 4;
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
brush, leftPos, topPos, strFormat);
}
}
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border);
e.Handled = true;
}
चाल मौजूदा `पेंट विधि सेल के अधिकांश के चित्र संभाल जाने के लिए है: सौभाग्य से वहाँ एक neater समाधान है। हम केवल पाठ को चित्रित करते हैं। सीमा को पाठ के बाद चित्रित किया गया है क्योंकि मैंने पाया कि अन्यथा, पाठ को कभी-कभी सीमा पर चित्रित किया जाएगा, जो खराब दिखता है।