C# DataGridView,大单元格:内容永远不完全可见,滚动会跳过单元格

发布于 2024-08-08 22:08:19 字数 3064 浏览 2 评论 0原文

DataGridViewCell 大于 DataGridView 本身时,我在使用 DataGridView 控件(Windows.Forms、.NET Framework 3.0)时遇到了一个相当棘手的问题。当大单元格滚动到视图中时,它会正常显示,但由于它比视图大,因此在底部被切断。如果您进一步向下滚动,它最终会“捕捉”到顶部并停留在那里,直到达到某个阈值。然后,下一行将显示在顶部,并且“大”行消失。

因此,您永远无法完全看到大单元格的内容。

这是一个示例代码:

using System;
using System.Windows;

namespace LoggerTextBox {
public class TestForm : Form
{
    public TestForm()
    {
        Text = "DataGridView Large Cell Example";
        SetBounds(0, 0, 300, 200, BoundsSpecified.Width | BoundsSpecified.Height);

        DataGridView dataGridView = new DataGridView();
        dataGridView.Dock = DockStyle.Fill;
        dataGridView.ScrollBars = ScrollBars.Both;
        dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
        Controls.Add(dataGridView);

        DataGridViewColumn column = new DataGridViewTextBoxColumn();
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        column.CellTemplate.Style.WrapMode = DataGridViewTriState.True;
        dataGridView.Columns.Add(column);

        // normal row
        DataGridViewRow row = new DataGridViewRow();
        DataGridViewCell cell = (DataGridViewTextBoxCell)column.CellTemplate.Clone();
        cell.Value = "Foo";
        row.Cells.Add(cell);
        dataGridView.Rows.AddRange(row);

        // multiline row
        row = new DataGridViewRow();
        cell = (DataGridViewTextBoxCell)column.CellTemplate.Clone();
        cell.Value =
            "Lorem ipsum dolor sit amet, consetetur sadipscing elitr," + Environment.NewLine +
            "sed diam nonumy eirmod tempor invidunt ut labore et doloreLorem," + Environment.NewLine +
            "ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy," + Environment.NewLine +
            "eirmod tempor invidunt ut labore et dolore magna aliquyam erat,," + Environment.NewLine +
            "sed diam voluptua. At vero eos et accusam et justo duo dolores et," + Environment.NewLine +
            "ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est," + Environment.NewLine +
            "Lorem ipsum dolor sit amet. magna aliquyam erat, sed diam voluptua.," + Environment.NewLine +
            "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita," + Environment.NewLine +
            "kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
        row.Cells.Add(cell);
        dataGridView.Rows.AddRange(row);

        // normal row
        row = new DataGridViewRow();
        cell = (DataGridViewTextBoxCell)column.CellTemplate.Clone();
        cell.Value = "Bar";
        row.Cells.Add(cell);
        dataGridView.Rows.AddRange(row);
    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new TestForm());
    }
}
} // namespace

知道如何解决这个问题吗?

I have encountered a rather nasty problem with the DataGridView control (Windows.Forms, .NET Framework 3.0) when there is a DataGridViewCell that is larger than the DataGridView itself. When the large cell is scrolled into view it displays normally, cut off at the bottom since it is larger than the view. If you scroll down further it eventually "snaps" at the top and stays there, until you reach a certain threshold. Then, the next row will be displayed at the top and the "large" row disappears.

Because of that you are never able to fully see the contents of the large cell.

Here's an example code:

using System;
using System.Windows;

namespace LoggerTextBox {
public class TestForm : Form
{
    public TestForm()
    {
        Text = "DataGridView Large Cell Example";
        SetBounds(0, 0, 300, 200, BoundsSpecified.Width | BoundsSpecified.Height);

        DataGridView dataGridView = new DataGridView();
        dataGridView.Dock = DockStyle.Fill;
        dataGridView.ScrollBars = ScrollBars.Both;
        dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
        Controls.Add(dataGridView);

        DataGridViewColumn column = new DataGridViewTextBoxColumn();
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        column.CellTemplate.Style.WrapMode = DataGridViewTriState.True;
        dataGridView.Columns.Add(column);

        // normal row
        DataGridViewRow row = new DataGridViewRow();
        DataGridViewCell cell = (DataGridViewTextBoxCell)column.CellTemplate.Clone();
        cell.Value = "Foo";
        row.Cells.Add(cell);
        dataGridView.Rows.AddRange(row);

        // multiline row
        row = new DataGridViewRow();
        cell = (DataGridViewTextBoxCell)column.CellTemplate.Clone();
        cell.Value =
            "Lorem ipsum dolor sit amet, consetetur sadipscing elitr," + Environment.NewLine +
            "sed diam nonumy eirmod tempor invidunt ut labore et doloreLorem," + Environment.NewLine +
            "ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy," + Environment.NewLine +
            "eirmod tempor invidunt ut labore et dolore magna aliquyam erat,," + Environment.NewLine +
            "sed diam voluptua. At vero eos et accusam et justo duo dolores et," + Environment.NewLine +
            "ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est," + Environment.NewLine +
            "Lorem ipsum dolor sit amet. magna aliquyam erat, sed diam voluptua.," + Environment.NewLine +
            "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita," + Environment.NewLine +
            "kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
        row.Cells.Add(cell);
        dataGridView.Rows.AddRange(row);

        // normal row
        row = new DataGridViewRow();
        cell = (DataGridViewTextBoxCell)column.CellTemplate.Clone();
        cell.Value = "Bar";
        row.Cells.Add(cell);
        dataGridView.Rows.AddRange(row);
    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new TestForm());
    }
}
} // namespace

Any idea how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-08-15 22:08:19

我会截断超出特定大小的任何单元格内容(用省略号表示截断),并允许单击该单元格以显示一个弹出窗口,其中在可滚动窗口中显示完整内容。或者,如果文本超出一定长度,我将在自定义用户控件中呈现这些可能较大的单元格的内容,该自定义用户控件本身包含滚动条。

您遇到的问题是由于 DataGridView 以非预期的方式使用而导致的,因此对于没有简单的内置方法来处理此问题,我并不感到惊讶。

更新:对于查看日志,ReportViewer可能是更合适的控件。以下是有关使用它的一些链接:

http://www.codeproject.com/KB/ cs/reportdisplay.aspx

http://www.microsoft.com/Downloads/details.aspx?FamilyID=f38f7037-b0d1-47a3-8063-66af555d13d9&displaylang=en

http://www.devx.com/dotnet/Article/30424/

I would truncate any cell's contents beyond a certain size (with ellipses to indicate the truncation) and allow the cell to be clicked to display a pop-up window with the full contents visible in a scrollable window. Or I would render the contents of these potentially large cells in a custom UserControl that itself contains scrollbars if the text is beyond a certain length.

You're running into a problem that results from the DataGridView being used in an unintended way, so I'm not surprised that there's no simple, built-in way of dealing with this.

Update: for viewing logs, the ReportViewer might be a more suitable control. Here are some links about using it:

http://www.codeproject.com/KB/cs/reportdisplay.aspx

http://www.microsoft.com/Downloads/details.aspx?FamilyID=f38f7037-b0d1-47a3-8063-66af555d13d9&displaylang=en

http://www.devx.com/dotnet/Article/30424/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文