Datagridview单元工具提示自动换行

时间:2015-11-18 08:54:37

标签: c# winforms datagridview tooltip

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

2 个答案:

答案 0 :(得分:0)

试试这个例子:

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;
    }
}

答案 1 :(得分:0)

尝试一下-它对我有用:

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";
                    }
                }
            }
        }
相关问题