Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 341
sunside Avatar asked Oct 26 '09 19:10

sunside


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

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/

like image 140
MusiGenesis Avatar answered Oct 11 '22 17:10

MusiGenesis