显示DevExpress网格单元的工具提示

时间:2014-10-13 14:22:00

标签: c# winforms gridview devexpress tooltip

我正在使用" DevExpress"网格,我想为每个单元格显示一个工具提示,因为我将光标悬停在它上面。我已为此编写了代码。它工作正常并显示工具提示。但是当我将光标移动到同一行时,工具提示不会改变。 (水平移动)。但是,如果我离开当前行并返回,则工具提示会发生变化。请告诉我。

以下是" toolTipController"的代码。 (我复制了整个方法以便更好地理解)

private void toolTipController1_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
    {
        bool validColumn = false;
        if (e.SelectedControl != gridControl1)
            return;

        GridHitInfo hitInfo = gridView1.CalcHitInfo(e.ControlMousePosition);

        if (hitInfo.InRow == false)
            return;

        if (hitInfo.Column == null)
            return;

        //concern only the following fields
        if (hitInfo.Column.FieldName == "Monday" || hitInfo.Column.FieldName == "Tuesday" || hitInfo.Column.FieldName == "Wednesday" || hitInfo.Column.FieldName == "Thursday" || hitInfo.Column.FieldName == "Friday")
            validColumn = true;

        if (!validColumn)
            return;

        string toolTip = string.Empty;
        SuperToolTipSetupArgs toolTipArgs = new SuperToolTipSetupArgs();
        toolTipArgs.Title.Text = string.Empty;

        //Get the data from this row
        string columnCaption = hitInfo.Column.Caption;
        DateTime dateOK = new DateTime(2000,1,1);
        if (DateTime.TryParse(columnCaption, out dateOK))
        {

            DateTime date = DateTime.Parse(columnCaption);
            int row = hitInfo.RowHandle;
            long teacherID = long.Parse(gridView1.GetRowCellValue(row, "TeacherID").ToString());

            GuaranteedDay gDay = db.GuaranteedDays.Where(p => p.Date == date && p.TeacherID == teacherID && p.Type == 5).FirstOrDefault();
            if (gDay != null)
            {
                if (gDay.Note != string.Empty)
                {
                    //Set description for the tool-tip
                    string description = string.Empty;
                    int type = gDay.Type;
                    switch (type)
                    {
                        case 1:
                            description = "guarantee offered";
                            break;
                        case 2:
                            description = "guaranteed";
                            break;
                        case 3:
                            description = "texted";
                            break;
                        case 4:
                            description = "available";
                            break;
                        case 5:
                            description = "unavailable";
                            break;
                    }
                    //Add Notes & description for the tool-tip
                    toolTip = "Notes : " + gDay.Note + "\nDescription : " + description;

                    string BodyText = toolTip;

                    toolTipArgs.Contents.Text = BodyText;
                    e.Info = new ToolTipControlInfo();
                    e.Info.Object = hitInfo.HitTest.ToString() + hitInfo.RowHandle.ToString(); 
                    e.Info.ToolTipType = ToolTipType.SuperTip;
                    e.Info.SuperTip = new SuperToolTip();
                    e.Info.SuperTip.Setup(toolTipArgs);
                }
            }
        }
    }
}

感谢您的帮助, Kushan Randima。

1 个答案:

答案 0 :(得分:4)

  

但是当我将光标移动到同一行时,工具提示没有改变。 (水平移动)。   但如果我离开当前行并返回,则工具提示会发生变化。

我看到你为当前行中的任何单元传递了相同的“命中对象”:

e.Info.Object = hitInfo.HitTest.ToString() + hitInfo.RowHandle.ToString();  

要完成任务,您应该为不同的单元传递不同的“命中对象”:

e.Info.Object = hitInfo.HitTest.ToString() + hitInfo.RowHandle.ToString() + hitInfo.Column.FieldName;