views:

1082

answers:

2

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?

+1  A: 

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&amp;displaylang=en

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

MusiGenesis
I need something more sophisticated than a textbox logging purposes (to display the log output, to be exact) and figured the DataGridView (bound to a list of log items) would be a good idea. I bypassed this scrolling issue now by splitting the messages into several rows beforehand. Not exactly what I want because that introduces other problems, but ... okay.Thanks!
Markus
@Markus: you do all your logging in Latin? :)
MusiGenesis
@Markus: I recommend asking your problem as another question here on SO, and see what people suggest. Something like "what is best control for displaying (potentially very long) log items in a winforms app?" I can't really think of a good answer, myself. I'd probably end up writing a complete custom control for this, but that's potentially a lot of work.
MusiGenesis
@MusiGenesis Only sometimes, if my colleagues aren't looking. :)I'll have a look at the ReportViewer, but it doesn't seem like "the" solution to me. (The thing is that I'm looking at status outputs from different programs in the network that are coming in "live".)Asking another question might be a good idea. (It's just that everybody seems to answer "list box", which really isn't what I need :))
Markus
MusiGenesis
@MusiGenesis Heh heh.
Markus
A: 

This is a duplicate of this question: http://stackoverflow.com/questions/830211/how-do-i-make-a-datagridview-that-doesnt-snap-its-rows-to-the-top-of-the-control See my answer there.

mhenry1384