.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Easy Web
Imran Ghani

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Add tooltip to datagridview cells in windows application

Posted By:Amit Mehra       Posted Date: May 15, 2013    Points: 40    Category: DataGrid    URL: http://www.dotnetspark.com  

This code sample will allow to show tooltip for a particular columns cell of DataGridView in a windows forms application. Code example is available in C# and VB.NET.
 

Sometime you would like to show tooltip for a cell in DataGridView in windows forms application. Below is the code to show add tool tip for a particular column all rows cells.

For adding tool tip we are going to use CellFormatting event of the DataGridView. Below is the

Code example in C#

private void myDataGridView_CellFormatting( object sender , DataGridViewCellFormattingEventArgs e )
{
    int columnIndex = 1; // Assign the column index you want to add tooltip

    if( e.ColumnIndex == myDataGridView.Columns[ columnIndex ].Index )
    {
        //Get the datagridview cell
        DataGridViewCell cell = myDataGridView.Rows[ e.RowIndex ].Cells[ e.ColumnIndex ];
        if( cell != null )
        {
            //You can add any tooltip text in below example I am taking the cell value itself to show as tooltip
            cell.ToolTipText = myDataGridView.Rows[ e.RowIndex ].Cells[ columnIndex ].Value.ToString( );
        }
    }
}

Code Sample in VB.Net

Private Sub myDataGridView_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
	Dim columnIndex As Integer = 1
	' Assign the column index you want to add tooltip
	If e.ColumnIndex = myDataGridView.Columns(columnIndex).Index Then
		'Get the datagridview cell
		Dim cell As DataGridViewCell = myDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
		If cell IsNot Nothing Then
			'You can add any tooltip text in below example I am taking the cell value itself to show as tooltip
			cell.ToolTipText = myDataGridView.Rows(e.RowIndex).Cells(columnIndex).Value.ToString()
		End If
	End If
End Sub

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend