I use dapfor .Net Grid. The following code should be of help.
public class RatingEditor : UITypeEditorEx
{
public override bool GetPaintCellSupported()
{
//The cell should be repainted in the editor
return true;
}
public override void PaintCell(PaintCellEventArgs e)
{
//Do basic painting without text
e.Text = string.Empty;
e.PaintAll();
e.Handled = true;
//Draw the stars over the cell
Rectangle bounds = e.Cell.VirtualBounds;
bounds.Width = Resources.star_grey.Width;
for (int i = 0; i < 5; i++)
{
//Select image
if (e.Cell.Value != null && e.Cell.Value is int)
{
int curRating = (int) e.Cell.Value;
Image image = i >= curRating ? Resources.star_grey : Resources.star_yellow;
e.Graphics.DrawImage(image, bounds);
bounds.X += bounds.Width;
}
}
}
public override StopEditReason EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
{
//Edit the cell if the user has clicked on the cell with the left button
if (cell.Row != null && Equals(StartEditReason.LButtonClick, reason))
{
Point pt = cell.Row.Grid.PointToClient(Cursor.Position);
Rectangle bounds = cell.VirtualBounds;
int imageWidth = Resources.star_grey.Width;
if (bounds.Contains(pt) && imageWidth > 0)
{
//Set a new rating to the data object
cell.Value = ((pt.X - bounds.X)/imageWidth) + 1;
cell.Invalidate();
}
}
return StopEditReason.UserStop;
}
}
Maplecurie, if this helps please login to Mark As Answer. | Alert Moderator