I have the following cell which is used for my custom column data type on my data grid.
public class DataGridViewReviewerCell : DataGridViewCell
{
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return Value;
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates cellState, object value, object formattedValue,
string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, "", "", errorText,
cellStyle, advancedBorderStyle, paintParts);
var parent = (DataGridViewReviewerColumn) OwningColumn;
var columnValue = (ReviewerCheckBox) Value;
CheckBoxRenderer.DrawCheckBox(
graphics,
new Point(cellBounds.X + 4, cellBounds.Y + 4),
new Rectangle(cellBounds.X + 2, cellBounds.Y + 4, cellBounds.Width, cellBounds.Height - (4 * 2)),
" " + columnValue.ReviewerEmployeeName,
parent.InheritedStyle.Font,
TextFormatFlags.Left,
false,
(columnValue.IsChecked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal));
}
}
This class works as expected however every time Paint() gets called the text (currently " " + columnValue.ReviewerEmployeeName
) keeps getting layered creating very unreadable text. I can't seem to find anything that will fix this problem.
Slow piece of code
Here is the piece of code that is running very slowly. When I put this in debug mode it seems to be running endlessly.
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Rows.Add("STEP A", new ReviewerCheckBox());
dataGridView1.Rows.Add("STEP B", new ReviewerCheckBox());
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex != 1) return;
var cell = (ReviewerCheckBox) dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
cell.IsChecked = !cell.IsChecked;
cell.ReviewerEmployeeName = (cell.IsChecked ? Environment.UserName : String.Empty);
//MessageBox.Show("Testing");
}
}
public class ReviewerCheckBox
{
public string ReviewerEmp开发者_如何学CloyeeName { get; set; }
public bool IsChecked { get; set; }
public bool IsRequired { get; set; }
}
}
public class DataGridViewReviewerColumn : DataGridViewColumn
{
public DataGridViewReviewerColumn()
{
CellTemplate = new DataGridViewReviewerCell();
ReadOnly = false;
SortMode = DataGridViewColumnSortMode.NotSortable;
Resizable = DataGridViewTriState.False;
}
}
You should be checking the paintParts
argument to determine which parts you should be drawing. For example if the background needs drawing, the DataGridViewPaintParts.Backgound flag will be set.
if (paintParts.HasFlag(DataGridViewPaintParts.Background))
{
using (SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(cellBackground, cellBounds);
}
}
if (paintParts.HasFlag(DataGridViewPaintParts.Border))
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// Paint you cell content here
Here is a slightly more complete example
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
namespace HideMainWinForm
{
class DataGridViewReviewerCell : DataGridViewCell
{
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
return value;
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (paintParts.HasFlag(DataGridViewPaintParts.Background))
{
using (SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(cellBackground, cellBounds);
}
}
if (paintParts.HasFlag(DataGridViewPaintParts.Border))
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
if (value != null)
{
CheckBoxRenderer.DrawCheckBox(
graphics,
new Point(cellBounds.X + 4, cellBounds.Y + 4),
new Rectangle(cellBounds.X+24,cellBounds.Y+4, cellBounds.Width-24, cellBounds.Height-4),
formattedValue.ToString(),
OwningColumn.InheritedStyle.Font,
TextFormatFlags.Left,
false,
CheckBoxState.CheckedNormal);
}
}
}
}
The two HasFlag
function calls can be translated to
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
...
}
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
...
}
精彩评论