【问题标题】:MouseHover Event on DataGridView - Index Out of RangeDataGridView 上的 MouseHover 事件 - 索引超出范围
【发布时间】:2012-12-07 18:00:37
【问题描述】:

我有一个带有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);
    }
}

但是,只要表单加载和移动鼠标,我就会收到索引超出范围错误。我以前从未使用过此活动,所以如果有人能指出我哪里出错了,我将不胜感激。

【问题讨论】:

    标签: winforms c# winforms datagridview


    【解决方案1】:

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

        DataGridViewCell cell = (DataGridViewCell)grid[clientPos.X, clientPos.Y];
    

    按行和列索引而不是按屏幕坐标,因此您的屏幕坐标可能远高于网格中的行数或列数,因此导致 IndexOutOfRange 异常。

    您应该使用 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
    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多