I am binding a List to a DataGridView. One property of the SomeObject class will be a status code (ex. Red, Yellow, Green). Can I ‘bind’ the status to the background color of a cell easily? How about binding to a tooltip also?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can write a handler for the DataGridView’s CellFormatting event to customise the background colour. Here’s a worked example (you’ll need to have dragged a DataGridView onto the default Form then double-clicked on the CellFormatting event to create a handler):
The objects here just have a Status and Text for simplicity. I create a BindingSource for an example set of these objects, then use that as the data source for the DataGridView. By default, the grid automatically generates columns when you bind, so there’s no need to do that manually. I also hide the first column, which is bound to the Status value, as we’re going to colour the Text cells instead.
To actually do the painting, we respond to the CellFormatting event. We get a reference to the DataGridView by casting sender, then use the RowIndex property of the DataGridViewCellFormattingEventArgs object to get at the data item iteself (each Row has a DataBoundItem property that conveniently gives us this). As DataBoundItem is an object type, we need to cast it to our specific type, then we can actually get to the Status property itself…phew!
I haven’t had any experience with tooltip programming, but I would have thought that you should respond to the MouseHover event, then work on discovering which row is being pointed at to start with.
I hope this helps.