【问题标题】:Is it possible to have Multi-line DataGridView cells without wrapping text?是否可以在不换行的情况下使用多行 DataGridView 单元格?
【发布时间】:2012-12-31 13:21:41
【问题描述】:

我知道我可以在RowTemplateDefaultCellStyle 上将WrapMode 设置为true,但是这并没有给我想要的行为。我在每个单元格中显示一个字符串列表,因此我希望回车被识别,但我不希望长项目中的文本换行。

有谁知道这是否有可能实现?

【问题讨论】:

  • 是的,我也需要。不幸的是,启用换行还会删除“...”位,该位显示有更多文本可供查看。必须有办法解决这个问题 - 我会开放赏金。

标签: c# winforms datagridview


【解决方案1】:

我希望这是您正在寻找的:

我使用了两个事件:

  1. 我测量了单元格编辑后的高度。
  2. 我在绘制单元格时测量了文本,并在需要时对其进行修剪,然后重复直到合适为止。

代码:

public partial class Form1 : Form
{
    private readonly int _rowMargins;

    public Form1()
    {
        InitializeComponent();
        int rowHeight = dataGridView1.Rows[0].Height;
        _rowMargins = rowHeight - dataGridView1.Font.Height;
    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView view = sender as DataGridView;
        DataGridViewCell cell = view.Rows[e.RowIndex].Cells[e.ColumnIndex];
        string text = string.Format("{0}", cell.FormattedValue);
        if (!string.IsNullOrEmpty(text))
        {
            Size size = TextRenderer.MeasureText(text, view.Font);
            view.Rows[e.RowIndex].Height = Math.Max(size.Height + _rowMargins, view.Rows[e.RowIndex].Height);
        }
    }

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex == -1 || e.RowIndex == -1)
        {
            return;
        }
        e.Paint(e.ClipBounds, DataGridViewPaintParts.All ^ DataGridViewPaintParts.ContentForeground);

        DataGridView view = sender as DataGridView;

        string textToDisplay = TrimTextToFit(string.Format("{0}", e.FormattedValue), (int) (e.CellBounds.Width * 0.96), view.Font);

        bool selected = view.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
        SolidBrush brush = new SolidBrush(selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor);

        e.Graphics.DrawString(textToDisplay, view.Font, brush, e.CellBounds.X, e.CellBounds.Y + _rowMargins / 2);

        e.Handled = true;
    }

    private static string TrimTextToFit(string text, int contentWidth, Font font)
    {
        Size size = TextRenderer.MeasureText(text, font);

        if (size.Width < contentWidth)
        {
            return text;
        }

        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (i < text.Length)
        {
            sb.Append(text[i++]);
            size = TextRenderer.MeasureText(sb.ToString(), font);

            if (size.Width <= contentWidth) continue;

            sb.Append("...");

            while (sb.Length > 3 && size.Width > contentWidth)
            {
                sb.Remove(sb.Length - 4, 1);
                size = TextRenderer.MeasureText(sb.ToString(), font);
            }

            while (i < text.Length && text[i] != Environment.NewLine[0])
            {
                i++;
            }
        }
        return sb.ToString();
    }

}

享受,
关门

设计师代码:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.LineNumber = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.Content = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.AllowUserToDeleteRows = false;
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.LineNumber,
        this.Content});
        this.dataGridView1.Location = new System.Drawing.Point(13, 13);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.RowHeadersWidth = 55;
        this.dataGridView1.RowTemplate.DefaultCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
        this.dataGridView1.Size = new System.Drawing.Size(493, 237);
        this.dataGridView1.TabIndex = 0;
        this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
        this.dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
        // 
        // LineNumber
        // 
        this.LineNumber.FillWeight = 30F;
        this.LineNumber.Frozen = true;
        this.LineNumber.HeaderText = "#";
        this.LineNumber.MaxInputLength = 3;
        this.LineNumber.Name = "LineNumber";
        this.LineNumber.ReadOnly = true;
        this.LineNumber.Resizable = System.Windows.Forms.DataGridViewTriState.False;
        this.LineNumber.Width = 30;
        // 
        // Content
        // 
        this.Content.HeaderText = "Content";
        this.Content.Name = "Content";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(518, 262);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Is it possible to have Multi-line DataGridView cells without wrapping text?";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.DataGridViewTextBoxColumn LineNumber;
    private System.Windows.Forms.DataGridViewTextBoxColumn Content;
}

【讨论】:

  • 目前这对我不起作用。我将 WrapMode 设置为 true 还是 false?我将尝试编辑问题(即使它不是我自己的问题)并粘贴我目前拥有的代码。顺便说一句,我打算动态创建表格并通过代码(而不是来自用户)设置单元格内容,尽管两者对其他人来说可能都很方便。赏金现在很快就会用完,但我会再次打开它以鼓励像你这样的潜在好答案。
  • Dan W,使用 WrapMode = False。
  • Dan W,这当然只是一个演示,我用行号填充了第一列,并替换了“。”用 Enter 让我更容易。在 dataGridView1_CellEndEdit 上,只需要测量文本。问候,Ofir
  • 啊哈!我发现 - 它似乎对第二个专栏很有效,而不是第一个或其他专栏。你能调整代码让它们也能正常工作吗?
  • 我已经把到目前为止的代码放在这里:skytopia.com/stuff/table-code.txt
【解决方案2】:

我测试了这段代码,结果很好,请测试一下:

注意:在创建表单和Datagrid时,设置以下datagrid属性

1- AutoSizeRowsMo​​de 到 AllCells。
2- 目标列的 WrapMode 为 True

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DGMultiLine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int max = 12; //min Column Width in Char
            //do this for all rows of column for max Line Size in values

            string str = "Hello\r\nI am mojtaba\r\ni like programming very very \r\nrun this code and  pay attention to result\r\n Datagrid Must show this Line Good are you see whole of this Thats Finished!";
            string[] ss = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            //find max Line Size To Now
            for (int i = 0; i < ss.Length; i++)
                if (ss[i] != null && ss[i] != "")
                    if (ss[i].Length > max)
                        max = ss[i].Length;
            //Set target Column Width
            dataGridView1.Columns[0].Width = max*5;//for adequate value you must refer to screen resolution
            //filling datagrigView for all values
            dataGridView1.Rows[0].Cells[0].Value = str;
        }

    }
}

您可以通过真正的全文添加一个隐藏的列和一个可见的列,这些列通过上面的代码显示字符串值,但字符串行的大小超过了您的 Max Column Size 剪辑字符串,并将 ... 添加到 end 。 (如果不清楚请写完整的代码)

【讨论】:

  • 您好,谢谢,但看起来好像只适用于固定宽度的字体。此外,用户可能会在某个阶段调整列的大小,即使有代码,我也会担心这可能会减慢非常大的表的速度......
  • 还要补充一点——我希望列宽保持在一个恒定的小值,因为某些单元格可能包含非常长的文本行。因此,如果线条超过单元格宽度,则会显示“...”。
【解决方案3】:

通过将 \r\n 与字符串一起执行,它对我有用。

例如“你好”+ \r\n。然后它转到下一行。

编辑:

刚刚看到这是 WinForms。上述技巧仅适用于 WPF。

EDIT2:

你可以使用:

dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

如果您不想包装长物品,您只需这样做:

String stringTest = "1234567891";
if (stringTest.Length > 8)
{
   stringTest = stringTest.Replace(stringTest.Substring(8), "...");
}

如果字符串长于 8,这将添加“...”。

【讨论】:

  • 试过了 - 对我不起作用。我使用了代码dataGridView1[0, 0].Value = "Hello and welcome to" + "\r\n" + "Earth";。确保关闭 WrapMode 以进行测试。
  • 感谢您提供新信息。我没有说清楚,但实际上,我希望支持可变宽度字体(以获得更好的国际支持),并且用户可以调整对于非常大的表格可能会很慢的列。
【解决方案4】:

一种方法是您可以只显示一些单词,然后将鼠标悬停在该单元格上时,可以在工具提示中显示全文。

【讨论】:

    【解决方案5】:

    如果不将 WrapMode 设置为 true,我还没有找到一种方法。但是,您应该能够通过将单元格的宽度设置为足够宽以在一行上显示所有项目来“欺骗”DataGridView 执行此操作。

    Here's 使用 ComboBo 完成此操作的示例。

    【讨论】:

    • 谢谢,虽然我希望单元格宽度更小,以便更紧凑地显示数据,而且由于潜在的行可能非常长。
    【解决方案6】:

    这个类获取DataGridView 实例并添加修剪多行省略号(...) 的宽度和高度的行为。

    用法:

    MultilineTriming.Init(ref dataGridView); // that's it!
    

    享受,

    public static class MultilineTriming
    {
        private static int _rowMargins;
    
        public static void Init(ref DataGridView dataGridView)
        {
            dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            dataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.False;
    
            _rowMargins = dataGridView.RowTemplate.Height - dataGridView.Font.Height;
    
            Unregister(dataGridView);
    
            dataGridView.CellEndEdit += DataGridViewOnCellEndEdit;
            dataGridView.CellPainting += DataGridViewOnCellPainting;
            dataGridView.RowsAdded += DataGridViewOnRowsAdded;
            dataGridView.Disposed += DataGridViewOnDisposed;
        }
    
        private static void DataGridViewOnRowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            DataGridView view = sender as DataGridView;
            DataGridViewRow row = view.Rows[e.RowIndex];
            foreach (DataGridViewCell cell in row.Cells)
            {
                if (cell.FormattedValue == null)
                {
                    continue;
                }
                Size size = TextRenderer.MeasureText((string)cell.FormattedValue, view.Font);
                row.Height = Math.Max(size.Height + _rowMargins, row.Height);
            }
        }
    
        private static void DataGridViewOnDisposed(object sender, EventArgs eventArgs)
        {
            DataGridView dataGridView = sender as DataGridView;
            Unregister(dataGridView);
    
        }
    
        public static void Unregister(DataGridView dataGridView)
        {
            dataGridView.RowsAdded -= DataGridViewOnRowsAdded;
            dataGridView.CellEndEdit -= DataGridViewOnCellEndEdit;
            dataGridView.CellPainting -= DataGridViewOnCellPainting;
        }
    
        private static void DataGridViewOnCellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView view = sender as DataGridView;
            DataGridViewRow row = view.Rows[e.RowIndex];
            DataGridViewCell cell = row.Cells[e.ColumnIndex];
    
            string text = (string)cell.FormattedValue;
    
            if (string.IsNullOrEmpty(text)) return;
    
            Size size = TextRenderer.MeasureText(text, view.Font);
            row.Height = Math.Max(size.Height + _rowMargins, row.Height);
        }
    
        private static void DataGridViewOnCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1 || e.FormattedValue == null)
            {
                return;
            }
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All ^ DataGridViewPaintParts.ContentForeground);
    
            DataGridView view = sender as DataGridView;
    
            string textToDisplay = TrimTextToFit(string.Format("{0}", e.FormattedValue), (int)(e.CellBounds.Width * 0.96) - 3, e.CellBounds.Height - _rowMargins, view.Font);
    
            bool selected = view.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
            SolidBrush brush = new SolidBrush(selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor);
    
            e.Graphics.DrawString(textToDisplay, view.Font, brush, e.CellBounds.X + 1, e.CellBounds.Y + _rowMargins / 2);
    
            e.Handled = true;
        }
    
        private static string TrimTextToFit(string text, int contentWidth, int contentHeight, Font font)
        {
            Size size = TextRenderer.MeasureText(text, font);
    
            if (size.Width < contentWidth && size.Height < contentHeight)
            {
                return text;
            }
    
            int i = 0;
            StringBuilder sb = new StringBuilder();
            while (i < text.Length)
            {
                sb.Append(text[i++]);
                size = TextRenderer.MeasureText(sb.ToString(), font);
    
                if (size.Width < contentWidth) continue;
    
                sb.Append("...");
    
                while (sb.Length > 3 && size.Width >= contentWidth)
                {
                    sb.Remove(sb.Length - 4, 1);
                    size = TextRenderer.MeasureText(sb.ToString(), font);
                }
    
                while (i < text.Length && text[i] != Environment.NewLine[0])
                {
                    i++;
                }
            }
            string res = sb.ToString();
    
            if (size.Height <= contentHeight)
            {
                return res;
            }
    
            string[] lines = res.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            i = lines.Length;
            while (i > 1 && size.Height > contentHeight)
            {
                res = string.Join(Environment.NewLine, lines, 0, --i);
                size = TextRenderer.MeasureText(res, font);
            }
    
            return res;
        }
    }
    

    【讨论】:

      【解决方案7】:
             if ((!e.Value.Equals("OK")) && e.ColumnIndex == 6)
              {
                  e.CellStyle.WrapMode = DataGridViewTriState.True;
                  //dgvObjetivos.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                  dgvObjetivos.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
              }
      

      http://kshitijsharma.net/2010/08/23/showing-multiline-string-in-a-datagridview-cell/

      【讨论】:

        猜你喜欢
        • 2023-02-07
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 2011-10-16
        • 2019-07-08
        • 2019-04-05
        • 1970-01-01
        相关资源
        最近更新 更多