簡體   English   中英

當鼠標位於數據網格中時,顯示數據網格行中每個項目的工具提示

[英]Showing tool tip for every item in datagridview row when mouse is above it

你怎么能顯示工具提示datagridview對每一個項目datagridview當你特定的行中鼠標懸停在項目上?

我的表product有列:

product name 
product price 
product description
product image ....

我要求我有一個帶有列的datagridview ,我從數據庫中獲取這些:

product name 
product price 
product image ....

現在我想顯示這樣的工具提示:如果我將鼠標懸停在產品圖像上,將顯示該產品的產品說明。 我想為每一行做這件事。 有人請幫忙嗎?

查看DataGridViewCell.ToolTipText屬性並使用DataGridView的CellFormatting事件來設置此屬性值。 您可以使用事件的DataGridViewCellFormattingEventArgs ColumnIndex屬性來確定是否為要為其設置工具提示的列觸發事件,如果是,則使用事件的RowIndex指定該工具提示的值。

我鏈接的MSDN文章中的示例有一個很好的使用示例,但您的代碼可能如下所示:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
    if (e.ColumnIndex == dataGridView1.Columns[nameOrIndexOfYourImageColumn].Index) {
        var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        // Set the Cell's ToolTipText.  In this case we're retrieving the value stored in 
        // another cell in the same row (see my note below).
        cell.ToolTipText = dataGridView1.Rows[e.RowIndex].Cells[nameOrIndexOfYourDescriptionColumn].Value.ToString();
    }
}

哪里:
nameOrIndexOfYourImageColumn =圖像列nameOrIndexOfYourDescriptionColumn的列名稱或索引值=包含描述數據的列名稱或索引值。

注意:您需要某種方法來檢索行的描述數據。 執行此操作的常用方法是在DataGridView中為其創建一個列,但是因為您不希望顯示此列,所以將其Visible屬性設置為false。 但是還有其他選擇。

填充datagridview ,只需將單元格的TooltipText屬性設置為要顯示的文本。

我通過在每個DataGridViewCellTag屬性中存儲要在每個單元格的工具提示中顯示的文本來完成此操作。

然后在DataGridView.CellMouseEnter事件中,您可以看到鼠標在哪個單元格中使用DataGridViewCellEventArgs.ColumnIndexDataGridViewCellEventArgs.RowIndex值,並使用ToolTip.SetToolTip將相應單元格中的文本設置為工具提示文本。

如果工作得很好。

像這樣的東西:

private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= 0 & e.RowIndex >= 0) 
    {
        ToolTip1.SetToolTip(dgv, Convert.ToString(dgv.Item(e.ColumnIndex, e.RowIndex).Tag));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM