C# DataGridViewCheckBoxColumn上未显示工具提示
我正在动态地向我的C# DataGridViewCheckBoxColumn上未显示工具提示,c#,winforms,C#,Winforms,我正在动态地向我的DataGridView添加一个DataGridViewCheckBoxColumn 但是,我似乎无法将工具提示添加到复选框: // Method to Populate the DataGrid private void PopulateDataGrid(objPatient patient) { this.uiDocumentDataGrid.DataSource = DataManager.GetDocumentData(patient);
DataGridView
添加一个DataGridViewCheckBoxColumn
但是,我似乎无法将工具提示添加到复选框:
// Method to Populate the DataGrid
private void PopulateDataGrid(objPatient patient)
{
this.uiDocumentDataGrid.DataSource = DataManager.GetDocumentData(patient);
// Hide unnecessary columns
this.uiDocumentDataGrid.Columns["Forename"].Visible = false;
this.uiDocumentDataGrid.Columns["Surname"].Visible = false;
// Add column for selection
DataGridViewCheckBoxColumn selectedColumn = new DataGridViewCheckBoxColumn();
selectedColumn.Name = "Selected";
selectedColumn.HeaderText = "Attach";
selectedColumn.ReadOnly = false;
this.uiDocumentDataGrid.Columns.Insert(0,selectedColumn);
// Set columns except checkbox to readonly
foreach(DataGridViewColumn c in this.uiDocumentDataGrid.Columns)
{
if (c.Index > 0)
{
c.ReadOnly = true;
}
}
// Refresh the view in case of draw issues
this.uiDocumentDataGrid.Refresh();
// Add tooltip to each checkbox
foreach (DataGridViewRow r in uiDocumentDataGrid.Rows)
{
r.Cells["Selected"].ToolTipText = "Check this box to select this document.";
}
// Disable the functionality button if no rows.
if (this.uiDocumentDataGrid.RowCount == 0)
{
this.uiSendButton.Enabled = false;
}
}
此方法不会显示任何工具提示。我是不是错过了一些明显的东西?试试这个
DataGridViewCheckBoxColumn selectedColumn = new DataGridViewCheckBoxColumn();
selectedColumn.Name = "Selected";
selectedColumn.HeaderText = "Attach";
selectedColumn.ReadOnly = false;
this.uiDocumentDataGrid.Columns.Insert(0,selectedColumn);
uiDocumentDataGrid.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(uiDocumentDataGrid_CellToolTipTextNeeded);
在事件处理程序中
void uiDocumentDataGrid_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
if (e.ColumnIndex == 0)
e.ToolTipText = "Check this box to select this document.";
}
试试这个
DataGridViewCheckBoxColumn selectedColumn = new DataGridViewCheckBoxColumn();
selectedColumn.Name = "Selected";
selectedColumn.HeaderText = "Attach";
selectedColumn.ReadOnly = false;
this.uiDocumentDataGrid.Columns.Insert(0,selectedColumn);
uiDocumentDataGrid.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(uiDocumentDataGrid_CellToolTipTextNeeded);
在事件处理程序中
void uiDocumentDataGrid_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
if (e.ColumnIndex == 0)
e.ToolTipText = "Check this box to select this document.";
}
不要尝试在窗体的构造函数中设置单元格的工具提示值。请尝试在加载或显示的事件中设置它们:
protected override void OnLoad(EventArgs e) {
foreach (DataGridViewRow r in uiDocumentDataGrid.Rows)
{
r.Cells["Selected"].ToolTipText = "Check this box to select this document.";
}
base.OnLoad(e);
}
不要尝试在窗体的构造函数中设置单元格的工具提示值。请尝试在加载或显示的事件中设置它们:
protected override void OnLoad(EventArgs e) {
foreach (DataGridViewRow r in uiDocumentDataGrid.Rows)
{
r.Cells["Selected"].ToolTipText = "Check this box to select this document.";
}
base.OnLoad(e);
}
将此代码添加到
CellFormatting
事件中为我排序。我为子孙后代添加了这个答案,但也会接受拉尔斯泰克的答案,因为他也是他的作品,正是他在安威向我指出了这个事件
private void uiDocumentDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.ColumnIndex == 0)
{
DataGridViewCell cell = this.uiDocumentDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.ToolTipText = "Check this box to select this document.";
}
}
将此代码添加到
CellFormatting
事件中为我排序。我为子孙后代添加了这个答案,但也会接受拉尔斯泰克的答案,因为他也是他的作品,正是他在安威向我指出了这个事件
private void uiDocumentDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.ColumnIndex == 0)
{
DataGridViewCell cell = this.uiDocumentDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.ToolTipText = "Check this box to select this document.";
}
}
当鼠标移到列标题上时,工具提示就会出现。@LarsTech-是的,我的第一个代码示例就是这样。第二个代码块是否应该正确执行此操作?为每个复选框执行此操作所需的代码是什么?这篇MSDN文章建议使用
CellFormatting
事件:我用foreach(…)
尝试了您的第二个代码片段,它对我起了作用。显然,这只有在添加行之后才起作用。到目前为止,我唯一可以复制它的方法是,如果我在构造函数中设置了工具提示,那么它就不起作用了。如果在窗体的加载事件中添加工具提示,一切正常。当鼠标悬停在列标题上时,工具提示就会出现。@LarsTech-是的,我的第一个代码示例就是这样。第二个代码块是否应该正确执行此操作?为每个复选框执行此操作所需的代码是什么?这篇MSDN文章建议使用CellFormatting
事件:我用foreach(…)
尝试了您的第二个代码片段,它对我起了作用。显然,这只有在添加行之后才起作用。到目前为止,我唯一可以复制它的方法是,如果我在构造函数中设置了工具提示,那么它就不起作用了。如果我在表单的加载事件中添加工具提示,一切都很好。谢谢。我想知道为什么不能在窗体的构造函数中设置它们。谢谢。我想知道为什么不能在窗体的构造函数中设置它们;仅供参考:我已经将代码放在Form_Load中,并且它工作正常。很高兴您能够使用Lars Tech的解决方案工作;仅供参考:我已经将代码放在Form_Load中,并且成功了。