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

StackOverflow https://stackoverflow.com/questions/14805554

Question

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

Était-ce utile?

La 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);
        }
    }

Autres conseils

You can programmatically set the tooltip to anything that you can programmatically access. I'm not sure it's good programming practice though. The tool tip takes some time to appear.

In the following example I'm creating some dataset to populate a datagridview. I didn't use datetimes, but just composed the tooltip for the first column of the sum of the strings in columns 2 and 3.

 DataTable dt = new DataTable();

    private void button1_Click(object sender, EventArgs e)
    {
        //put some data into a table
        int[,] time = new int[,] { { 0, 4, 1 }, { 1, 5, 2 }, { 15, 10, 3 } };

        dt.Columns.Add("x");
        dt.Columns.Add("y");
        dt.Columns.Add("z");

        for (int i = 0; i < 3; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = time[i, 0];
            dr[1] = time[i, 1];
            dr[2] = time[i, 2];
            dt.Rows.Add(dr);
        }

       dataGridView1.DataSource = dt;

    }


    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.ShowCellToolTips = true;

        Point loc = dataGridView1.CurrentCellAddress;
        if (loc.X == 0)
        {
            dataGridView1.CurrentCell.ToolTipText = String.Format("{0} ",
                dt.Rows[loc.Y][loc.X + 1].ToString() + dt.Rows[loc.Y][loc.X + 2].ToString());
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top