Search code examples
c#winformsdatagridviewdatagridviewcolumn

In DatagridView, by column, I would like to have the column's ToolTipText gather data from other columns in the same row?


I have a column that has a time that the appointment starts and two to three others the length of the row's appointment, depending on the type. I would like to set the tooltip for the start time to show the ending time for that appointment equals the start time+columna.int+columnb.int+columnc.int. Is it possible in a relatively simple way to have the tooltip show the time the appointment should end? It would be based on another column in the same row.

Thanks for any help, Scott Patton


Solution

  • You can achieve your task in .MouseMove Event and .HitTest Method

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            var hitTest = dataGridView1.HitTest(e.X, e.Y);
            if (hitTest.Type == DataGridViewHitTestType.ColumnHeader)
            {
                List<string> data = new List<string>();
                //var getRows = dataGridView1.Rows.Cast<DataGridViewRow>().ToList();
                //foreach (var item in getRows)
                //    data.Add(item.Cells[1].EditedFormattedValue.ToString());
    
                if (dataGridView1.CurrentCell == null) return;
                var currentRowIndex = dataGridView1.CurrentCell.RowIndex;
                var getRows = dataGridView1.Rows[currentRowIndex].Cells.Cast<DataGridViewCell>().ToList();
                foreach (var item in getRows)
                    data.Add(item.EditedFormattedValue.ToString());
    
                string[] data1 = data.ToArray();
                dataGridView1.Columns[hitTest.ColumnIndex].ToolTipText = string.Join(", ", data1);
            }
        }