110,546
社区成员
发帖
与我相关
我的任务
分享
private void callListDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if ((e.ColumnIndex == 0) && (e.RowIndex >= 0))
{
// Draw Merged Cell
Graphics g = e.Graphics;
bool selected = ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected);
Color fcolor = (selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor);
Color bcolor = (selected ? e.CellStyle.SelectionBackColor : e.CellStyle.BackColor);
Font newfont = e.CellStyle.Font;
newfont = new Font(newfont, FontStyle.Bold);
// Get size information
string cust = this.callListDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
string subject = this.callListDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
Size newSize = TextRenderer.MeasureText(e.Graphics, cust, newfont);
// Calculate where to draw the content
int x = e.CellBounds.Left + e.CellStyle.Padding.Left;
int y = e.CellBounds.Top + e.CellStyle.Padding.Top;
int width = e.CellBounds.Width - (e.CellStyle.Padding.Left + e.CellStyle.Padding.Right);
int height = newSize.Height + (e.CellStyle.Padding.Top + e.CellStyle.Padding.Bottom);
// Draw background
g.FillRectangle(new SolidBrush(bcolor), e.CellBounds);
// Draw first line
TextRenderer.DrawText(e.Graphics, cust, newfont, new Rectangle(x, y, width, height), fcolor, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
// Use grey for second line if not selected
if (!selected)
{
fcolor = Color.Gray;
}
// Reset font and y location
newfont = e.CellStyle.Font;
y = y + height - 2;
TextRenderer.DrawText(e.Graphics, subject, newfont, new Rectangle(x, y, width, height), fcolor, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
e.Handled = true;
}
}