Reputation: 111
I have a Data Grid View like:
if (this.dgv.Rows.Count < 1)
{
this.dgv.DataSource = null;
this.dgv.DataBindings.Clear();
if (this.dgv.Columns.Count == 0) this.dgv.ColumnCount = 15;
this.dgv.ColumnHeadersVisible = true;
this.dgv.Columns[4].Name = "Added By";
this.dgv.Columns[5].Name = "AddedByFullName";
}
as you can see I have column 4 called Added By
this.dgv.Columns[4].Name = "Added By";
and column 5 called AddedByFullName
this.dgv.Columns[5].Name = "AddedByFullName";
I want to know how can I use AddedByFullName
column as a tooltip for Added By
column then I will just remove AddedByFullName
column , is that possible? Regards
Upvotes: 1
Views: 2341
Reputation: 530
You can do this using the CellMouseEnter
or CellToolTipTextNeeded
event for the DataGridView
. Hide the column you want to use as the source, then replace the control name in the sample to match your DataGridView
.
private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == dgv.Columns["Added By"].Index)
&& (e.RowIndex > -1))
{
dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = dgv.Rows[e.RowIndex].Cells[dgv.Columns["AddedByFullName"].Index].Value.ToString();
}
}
Upvotes: 3
Reputation: 123
There is a CellToolTipTextNeeded
event which is created specifically for setting tooltip text. You don't need to use use CellFormatting
or CellMouseEnter
. If you are going to show text of column 5 as tooltip of column 4, you can write:
private void g_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex >= 0)
{
e.ToolTipText = $"{dataGridView1[5, e.RowIndex].Value}";
}
}
Upvotes: 0
Reputation: 7111
The DataGridViewColumn
class has a ToolTipText
property. If you set it on the column, you will get a tool tip for the column header. If you want a tool tip to show up on every cell, you can implement a CellFormatting
event handler, pull out the right cell (from the column) and set the cell's ToolTipText
property. Something like:
private const int InterestingColumnNumber = 5;
private const string InterestingColumnToolTipText = "This Space For Rent";
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var senderGridView = sender as DataGridView;
if (senderGridView != null)
{
if (e.ColumnIndex == InterestingColumnNumber)
{
var cell = senderGridView.Rows[e.RowIndex].Cells[InterestingColumnNumber];
cell.ToolTipText = InterestingColumnToolTipText;
}
}
}
Upvotes: 0
Reputation: 111
Using CellMouseEnter
event can be a posibility to achieve this but it also can be done with CellFormatting
event as Microsoft REFERENCE
private void dgJobNotes_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == this.dgJobNotes.Columns["Added By"].Index)
&& e.Value != null)
{
dgJobNotes.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = dgJobNotes.Rows[e.RowIndex].Cells[5].Value.ToString();
}
Upvotes: 1