Rich P
Rich P

Reputation: 202

datagridview mousehover event how to determine which column you are on

I want to display a tooltip when I mousehover over a column header on my datagridview control (dgrv1). Currently, I can do the following, which works -- but -- my datagridview has several columns (and a lot of text for each tooltip), so I would like to call a method from the mousehover event. My problem is that I don't know how to capture which column I am hovering over to pass in to the method. Interestingly, the mousehover event picks the correct column in my current scenario:

private void dgrv1_MouseHover(object sender, EventArgs e)
{  
    dgrv1.Columns[1].ToolTipText = "column 1";
    dgrv1.Columns[2].ToolTipText = "column 2";
    dgrv1.Columns[3].ToolTipText = "column 3";
}

If I mousehover over column 1 -- the tooltiptext for column 1 gets displayed, and the same for columns 2, 3, ... But instead of listing 50 columns (which the tooltips would contain quite a bit of text) in the mousehover event here, how could I just call a method from the mousehover event and pass in the correct column number?

Upvotes: 3

Views: 7966

Answers (3)

Muhammad Saeed
Muhammad Saeed

Reputation: 81

try this:

Dim grvScreenLocation As Point = DGVRejected999Clm.PointToScreen(DGVRejected999Clm.Location)
            Dim tempX As Integer = DataGridView.MousePosition.X - grvScreenLocation.X + DGVRejected999Clm.Left
            Dim tempY As Integer = DataGridView.MousePosition.Y - grvScreenLocation.Y + DGVRejected999Clm.Top
            Dim hit As DataGridView.HitTestInfo = DGVRejected999Clm.HitTest(tempX, tempY)
            cellX = hit.RowIndex
            cellY = hit.ColumnIndex
            Dim col =DGVRejected999Clm.Columns(cellY).Name
            If col="INTERNAL_STATUS" THEN
            Dim value =DGVRejected999Clm.Rows(cellX).Cells(cellY).Value.ToString
               If Value="E" THEN
                   DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="999 ERROR"
                    Else If Value="F" Then
                      DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="EJECTED BY EDI SIMPLIFIED"
                    Else If value="N" Then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="NEW CLAIM"
                    Else If value="R" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="REGENERATED- WAITING FOR SUBMISSION"
                     Else If value="S" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="SUBMITTED"
                    Else If value="K" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="999 ACKNOWLEDGED"
                    Else If value="D" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="999 DENIED. RESUBMISSION REQUIRED"
                       Else If value="A" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="277 ACCEPTED"
                     Else If value="J" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="277 REJECTED"
                    Else If value="Q" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="REQUEUED"

                    Else If value="M" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="SEND TO PM-REJECTED by Payer"

                    Else If value="X" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="SEND TO PM-REJECTED by EDI Simplified"

                     Else If value="T" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="999 ACKNOWLEDGED-Payer dont support 277 - Payer Accepted"


                      Else If value="V" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="999 ACKNOWLEDGED-Payer dont support 277 - Payer Rejected"

                    Else If value="G" then
                    DGVRejected999Clm.Rows(cellX).Cells(cellY).ToolTipText="Rejected Claim - Review Required"



               End If
           End If 

Upvotes: 0

Reza Aghaei
Reza Aghaei

Reputation: 125207

You don't need to assign ToolTipText property of your columns in hover event. you simply can assign them using Designer or if you want when you load data or load event of form using such code:

foreach (DataGridViewColumn c in this.dataGridView1.Columns)
{
    c.ToolTipText = string.Format("Column {0}", c.Index + 1);
}

You can also assign a text to ToolTip property of a cell:

this.dataGridView1.Rows[0].Cells[0].ToolTipText = "Some text"

But if you want to know what is the column and row that mouse is hover on it, you can use HitTest method of DataGridView:

private void dataGridView1_MouseHover(object sender, EventArgs e)
{
    var p = this.dataGridView1.PointToClient(Cursor.Position);
    var info = this.dataGridView1.HitTest(p.X, p.Y);

    //You can use this values
    //info.ColumnX
    //info.RowY
    //info.ColumnIndex
    //info.RowIndex
}

Pay attention that RowIndex is -1 for column header cells and ColumnIndex is -1 for row header cells.

Upvotes: 1

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

Do it with the DataGridViewCellEventArgs:

private void DataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
  e.ColumnIndex //column
  e.RowIndex    //row
}

Upvotes: 1

Related Questions