Winforms DataGridView上的鼠标悬停事件-索引超出范围

Winforms DataGridView上的鼠标悬停事件-索引超出范围,winforms,c#,datagridview,Winforms,C#,Datagridview,我有一个带有DataGridView的应用程序,并且一直在尝试设置一个MouseHover事件,该事件提供有关悬停单元格的更详细信息 我的代码如下: private void dataCaseHistory_MouseHover(object sender, EventArgs e) { try { DataGridView grid = (DataGridView)sender; Point clientPos = grid.PointToCli

我有一个带有
DataGridView
的应用程序,并且一直在尝试设置一个
MouseHover
事件,该事件提供有关悬停单元格的更详细信息

我的代码如下:

private void dataCaseHistory_MouseHover(object sender, EventArgs e)
{
    try
    {
        DataGridView grid = (DataGridView)sender;
        Point clientPos = grid.PointToClient(Control.MousePosition);
        DataGridViewCell cell = (DataGridViewCell)grid[clientPos.X, clientPos.Y];
        int cellRow = cell.RowIndex;
        int cellColumn = cell.ColumnIndex;

        DataTable table = (DataTable)dataCaseHistory.DataSource;
        int docColumn = table.Columns.IndexOf("Doc");
        if (cellColumn == docColumn)
        {
            var varBundleID = table.Rows[cellRow]["BundleID"];
            if (varBundleID != DBNull.Value && varBundleID != null)
            {
                int bundleID = (int)varBundleID;
                cBundle bundle = new cBundle(bundleID);
                string header = "Bundle: '" + bundle.Name + "'";
                string body = "";
                foreach (DataRow row in bundle.DocumentBundle.Rows)
                {
                    int docID = (int)row["DocumentID"];
                    cDocument doc = new cDocument(docID);
                    body += doc.DocumentName + Environment.NewLine;
                }
                MessageBox.Show(body, header);
            }
            else
            {
                var varDocID = table.Rows[cellRow]["DocID"];
                if (varDocID != DBNull.Value && varDocID != null)
                {
                    int docID = (int)varDocID;
                    cDocument doc = new cDocument(docID);
                    string header = "Document";
                    string body = doc.DocumentName;
                    MessageBox.Show(body, header);
                }
            }
        }                
    }
    catch (Exception eX)
    {
        string eM = "Error occurred when Single Clicking a Document link in the History tab";
        aError err = new aError(eX, eM);
        MessageBox.Show(eX.Message, eM);
    }
}

但是,当窗体加载时,每当我移动鼠标时,就会出现索引超出范围的错误。我以前从未使用过此事件,因此如果有人能指出我的错误所在,我将不胜感激。

您在这行代码中访问的Item[]属性:

    DataGridViewCell cell = (DataGridViewCell)grid[clientPos.X, clientPos.Y];
是按行和列而不是按屏幕坐标索引的,因此屏幕坐标可能远高于网格中的行数或列数,因此导致IndexOutfrange异常

您应该使用HitTestInfo类获取单元格:

    MouseEventArgs args = (MouseEventaArgs) e;  
    DataGridView.HitTestInfo hitTest = this.grid.HitTest(args.X, args.Y);
    if (hitTest.Type == DataGridViewHitTestType.Cell)
    {
         DataGridViewCell cell = (DataGridViewCell)this.Grid[hitText.ColumnIndex, hitTest.RowIndex];
         // execute business logic here
    }

您还应该检查
hitTest.Type==DataGridViewHitTestType.Cell