[英]Add tooltip to specific column on Data Grid View
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
如你所见,我有第4列名为
Added By
this.dgv.Columns[4].Name = "Added By";
and column 5 called AddedByFullName
和第5列称为
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? 我想知道如何使用
AddedByFullName
列作为Added By
列的工具提示然后我将删除AddedByFullName
列,这可能吗? Regards 问候
You can do this using the CellMouseEnter
or CellToolTipTextNeeded
event for the DataGridView
. 您可以使用
DataGridView
的CellMouseEnter
或CellToolTipTextNeeded
事件来完成此操作。 Hide the column you want to use as the source, then replace the control name in the sample to match your DataGridView
. 隐藏要用作源的列,然后替换示例中的控件名称以匹配
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();
}
}
Using CellMouseEnter
event can be a posibility to achieve this but it also can be done with CellFormatting
event as Microsoft REFERENCE 使用
CellMouseEnter
事件可以实现这一目标,但也可以使用CellFormatting
事件作为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();
}
The DataGridViewColumn
class has a ToolTipText
property. DataGridViewColumn
类具有ToolTipText
属性。 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. 如果要在每个单元格上显示工具提示,可以实现
CellFormatting
事件处理程序,拉出正确的单元格(从列中)并设置单元格的ToolTipText
属性。 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;
}
}
}
There is a CellToolTipTextNeeded
event which is created specifically for setting tooltip text. 有一个
CellToolTipTextNeeded
事件,专门用于设置工具提示文本。 You don't need to use use CellFormatting
or CellMouseEnter
. 您不需要使用
CellFormatting
或CellMouseEnter
。 If you are going to show text of column 5 as tooltip of column 4, you can write: 如果要将第5列的文本显示为第4列的工具提示,则可以编写:
private void g_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex >= 0)
{
e.ToolTipText = $"{dataGridView1[5, e.RowIndex].Value}";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.