Partilhar via


Media player like rating DataGridView column

One request that I've seen a few times is to have a column in the DataGridView display a rating like stars similar to what you would find in Media Player. I've posted this over at the MSDN forums, but I thought having it in the blog would also be good.

Basically, this column/cell (called RatingColumn/RatingCell) derives from the DataGridViewImageColumn/ImageCell. This is done obviously since I am displaying an image (this is the formatted value/type), but the cell relies on integer data (0 through 5 for each star), so I change the ValueType for the column to be of type integer.

Since an integer cannot be converted to an image by magic, I override the GetFormattedValue and convert the integer value to the proper image. I use a simple array of images so the values 0 through 5 correctly index into the array. (I ended up creating 12 images in total - 6 images that go from 0 to 5 stars highlighted and another set of images that have a hot-highlight that I use for mouse over: and .) I also default the new row to the value of 3 (for three stars). Lastly, I override the Paint method to identify the star that the mouse is over. The logic to identify which star the mouse is over is a bit conviluted, but it works.

Anyway, here is the link to the unsupported sample (in c#): https://www.windowsforms.net/blogs/markrideout/RatingCustomDGVColumn.zip

One important note -- I did not add keyboard accessibility, only mouse.

-mark

Comments

  • Anonymous
    April 03, 2008
    Hi, Fantastic control! I'm fairly new to c# though and can't get it to work when bound to a datasource. Any advice? Thanks, Cal