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

sun*_*ide 6 .net c# datagridview scrollbar winforms

DataGridView当有一个DataGridViewCell大于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
Run Code Online (Sandbox Code Playgroud)

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

Mus*_*sis 1

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

您遇到的问题是由于 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/