Link to home
Start Free TrialLog in
Avatar of Amitava_Mukherjee
Amitava_MukherjeeFlag for India

asked on

C# code to show datagridview cell value on Balloon Tooltip

Please provide C# sample code to show datagrid view cell value on Balloon Tooltip
Avatar of rgn2121
rgn2121
Flag of United States of America image

You could probably use the CellMouseEnter event of the datagridview  and then use the eventargs of the event to get your value using e.ColumnIndex and e.RowIndex.
Once you get the value you could display that in a tooltip set as balloon style..
Avatar of Amitava_Mukherjee

ASKER

Please provide C# sample code
Here is VB code to get the info you need...I have never tried this so there might need to be some tweaks... I will try to convert as well..
 

    Private Sub DataGridView1_CellMouseEnter( _
                        ByVal sender As Object, _
                        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
                        Handles DataGridView1.CellMouseEnter
 
        If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then
 
            DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
            Dim s As String = DataGridView1.CurrentCell.Value
            MsgBox(s)
 
        End If
 
 
 
    End Sub

Open in new window

Paste the code below into the event I mentioned...
    if (e.RowIndex >= 0 & e.ColumnIndex >= 0) { 
        
        DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex); 
        string s = DataGridView1.CurrentCell.Value; 
            
        Interaction.MsgBox(s); 
    } 

Open in new window

Like I said...I didn't really do an error checking or validate much...good luck

    if (e.RowIndex >= 0 & e.ColumnIndex >= 0) { 
        
        DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex); 
            
        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ToolTipText = DataGridView1.CurrentCell.Value; 
    } 

Open in new window

Last time...this would be better..
    if (e.RowIndex >= 0 & e.ColumnIndex >= 0) { 
        
        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ToolTipText = DataGridView1.CurrentCell.Value; 
    } 

Open in new window

never mind that...I posted before I read the code...Go with the the second to last one.
 
Oh...and althought I am not certain, I think if you want a balloon tooltip then you need to add the tooltip control.  I am not sure you can change the type of tooltip built in to the grid...
The value show in Message Box [MessageBox.Show(s)], But not working in Balloon Tooltip, the default Tooltip show the value.
Shows in normal Tooltip, but not in Balloon Tooltip.

    if (e.RowIndex >= 0 & e.ColumnIndex >= 0) { 
        
        DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex); 
        this.mainToolTip.SetToolTip(DataGridView1, DataGridView1.CurrentCell.Value); 
            
        this.mainToolTip.Active = true; 
    } 

Open in new window

I dropped a tooltip control on the form and then change the IsBalloon property to design view as well as some of the display delays.
Using the code above it worked as expected...
Please send your sample project as an attachment
I didn't create a sample project...
ASKER CERTIFIED SOLUTION
Avatar of rgn2121
rgn2121
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am trying as per your instructions, but it is not working, please send a sample project containing the solution that you provided.
are you getting errors or what...
 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The call value Shows in normal Tooltip as well as Message Box, But NOT in Balloon Tooltip
First off, like I stated above, the default tooltip with the DataGridView should be set to FALSE.  If I don't do that on mine...it doesn't work.
I followed the same steps as above in an app I have, which already had a datasource for the grid, which I cannot upload.

sample.png
How you set the default tooltip value of DataGridView is FALSE.
Thankx..
Click on the DataGridView Control  in Design View.  Then in the Properties Window find the property called ShowCellToolTips.  Change it to False....
sample.png
Is it working now?
It is working
Instead of selecting almost every post I made as an answer, I think it would be more beneficial to those that come across this question if we split the points between ID: 24361883 (Tells how to setup the project) and ID: 24361947 (The final valid code posted.)
rgn