How can you show the tooltip for datagridview
for every item in datagridview
when you hover mouse over the item in that particular row?
I have table product
with columns:
product name
product price
product description
product image ....
I have a requirement that I have a datagridview
with columns and I am getting these from database:
product name
product price
product image ....
Now I want to show the tooltip like this: if I have mouse over the product image, the product description will be displayed for that product. I want to do this for every row. Would anyone please help on this one?
Take a look at the DataGridViewCell.ToolTipText property and use the DataGridView's
CellFormatting
event to set this property value. You can use the event'sDataGridViewCellFormattingEventArgs
ColumnIndex
property to determine if the event is firing for the column you want to set a tool tip for and if so use the event'sRowIndex
to specify that tool tip's value.The sample in the MSDN article I linked have a fine example of usage, but your code might look something like this:
Where:
nameOrIndexOfYourImageColumn
= the column name or index value of your image columnnameOrIndexOfYourDescriptionColumn
= the column name or index value with your description data.Note: that you'll need some way to retrieve a row's Description data. A common way to do this is to have a column for it in your DataGridView, but make since you don't want to display this column set its
Visible
property to false. There are other options however.I have done this by storing the text to show in the tooltip for each cell in the
Tag
property of eachDataGridViewCell
.Then in the
DataGridView.CellMouseEnter
event you can see in which cell the mouse is using theDataGridViewCellEventArgs.ColumnIndex
andDataGridViewCellEventArgs.RowIndex
values and set the text from the corresponding cell as the tooltip text usingToolTip.SetToolTip
.If works pretty well.
Something like this:
When filling the
datagridview
, just set theTooltipText
property of the cell to the text you want to display.