C# Datagridview单元格工具提示换行

C# Datagridview单元格工具提示换行,c#,winforms,datagridview,tooltip,C#,Winforms,Datagridview,Tooltip,我有一个datagridview,它在一列中有很长的字符串。当我将鼠标悬停在默认的一行上时,工具提示不足以显示整个内容。如何在datagridview工具提示中对文字进行自动换行?尝试以下示例: private void dgvProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if ((e.ColumnIndex == dgvProducts.Columns["c

我有一个datagridview,它在一列中有很长的字符串。当我将鼠标悬停在默认的一行上时,工具提示不足以显示整个内容。如何在datagridview工具提示中对文字进行自动换行?

尝试以下示例:

private void dgvProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{        
    if ((e.ColumnIndex == dgvProducts.Columns["colDescription"].Index) && e.Value != null)
    {
        DataGridViewCell cell = dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex];
        cell.ToolTipText = String.Join(Environment.NewLine, e.Value.ToString().DivideLongString());
    }
}

//-------------- Extension Methods ------------- C# 6.0

using static System.String;

public static class Extensions
{
    public static string[] DivideLongString(this string text, int length = 10)
    {
        if (text.Length <= length) return new[] { text };
        var words = text.GetTotalWords();
        var output = new string[(words.Length % length > 1 ? words.Length / length + 1 : words.Length % length < 1 ? words.Length / length - 1 : words.Length / length) + 100];
        var oIndex = 0;
        var wIndex = length - 1;
        try
        {
            for (var i = 0; i < words.Length; i++)
            {
                if (wIndex < i)
                {
                    wIndex += length - 1;
                    oIndex++;
                }
                output[oIndex] += $"{words[i]} ";
            }
            if (output.Length > 2 && !IsNullOrEmpty(output[output.Length - 1]) && output[output.Length - 1].CountWords() < 3)
            {
                output[output.Length - 2] += output[output.Length - 1];
                output[output.Length - 1] = Empty;
            }
            return output.Where(val => !IsNullOrEmpty(val)).ToArray();
        }
        catch (Exception ex)
        {
            ExceptionLogger.Log(ex);
            return output.Where(val => !IsNullOrEmpty(val)).ToArray();
        }
    }

    public static string ReplaceMultipleSpacesWith(this string text, string newString)
    {
        return Regex.Replace(text, @"\s+", newString);
    }

    public static string[] GetTotalWords(this string text)
    {
        text = text.Trim().ReplaceMultipleSpacesWith(" ");
        var words = text.Split(' ');
        return words;
    }
}
private void dgvProducts_CellFormatting(对象发送方,DataGridViewCellFormattingEventArgs e)
{        
if((e.ColumnIndex==dgvProducts.Columns[“colDescription”].Index)&&e.Value!=null)
{
DataGridViewCell cell=dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.ToolTipText=String.Join(Environment.NewLine,例如Value.ToString().DivideLongString());
}
}
//--------------扩展方法--------C#6.0
使用静态System.String;
公共静态类扩展
{
公共静态字符串[]DivideLongString(此字符串文本,int-length=10)
{
如果(text.Length 1?words.Length/Length+1:words.Length%Length<1?words.Length/Length-1:words.Length/Length)+100];
var-oIndex=0;
var wIndex=长度-1;
尝试
{
for(var i=0;i2&&!IsNullOrEmpty(output[output.Length-1])&&output[output.Length-1].CountWords()<3)
{
输出[output.Length-2]+=输出[output.Length-1];
输出[output.Length-1]=空;
}
返回输出。其中(val=>!IsNullOrEmpty(val)).ToArray();
}
捕获(例外情况除外)
{
例外日志(ex);
返回输出。其中(val=>!IsNullOrEmpty(val)).ToArray();
}
}
公共静态字符串ReplaceMultipleSpacesWith(此字符串文本,字符串新闻字符串)
{
返回Regex.Replace(文本@“\s+”,新闻字符串);
}
公共静态字符串[]GetTotalWord(此字符串文本)
{
text=text.Trim().ReplaceMultipleSpacesWith(“”);
var words=text.Split(“”);
返回单词;
}
}

试试这个-它对我很有用:

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            int wrapLen = 84;
            if ((e.RowIndex >= 0) && e.ColumnIndex >= 0)
            {
                DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                string cellText = cell.Value.ToString();
                if (cellText.Length >= wrapLen)
                {
                    cell.ToolTipText = "";
                    int n = cellText.Length / wrapLen;
                    for (int i = 0; i <= n; i++)
                    {
                        int wStart = wrapLen * i;
                        int wEnd = wrapLen * (i + 1);

                        if (wEnd >= cellText.Length)
                            wEnd = cellText.Length;

                        cell.ToolTipText += cellText.Substring(wStart, wEnd - wStart) + "\n";
                    }
                }
            }
        }
private void dataGridView1\u CellMouseEnter(对象发送方,DataGridViewCellEventArgs e)
{
int-wrapLen=84;
如果((e.RowIndex>=0)和&e.ColumnIndex>=0)
{
DataGridViewCell cell=this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
字符串cellText=cell.Value.ToString();
如果(cellText.Length>=wrapLen)
{
cell.ToolTipText=“”;
int n=cellText.Length/wrapLen;
for(int i=0;i=cellText.Length)
wEnd=cellText.Length;
cell.ToolTipText+=cellText.Substring(wStart,wEnd-wStart)+“\n”;
}
}
}
}

能否显示有关如何显示工具提示的代码?