开发者

C# DataGridView, large cells: Content never fully visible, scrolling skips cell

开发者 https://www.devze.com 2022-12-09 06:01 出处:网络
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

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.," +开发者_JAVA技巧 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?


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消