DataGridViewCell Multi-Line problem with long cell values.

C

Chris Shepherd

Another day, another problem with DataGridView display.

Here's the scenario: .NET 2.0, I have a WinForms application that
basically just allows a user to review comments on a particular item. It
doesn't matter what the item is, it could be a blog entry, a widget, or
a document, I wanted to simplify things by having one generic
UserControl that takes care of displaying and formatting the comments.
Simple enough, and it's been working fine mostly. The issue I've run
into is in how the DataGridView opts to scroll when a cell is being
wrapped. The scrollbar appears stuck on the top of the cell, and makes
it impossible to see what is in the entire field.

Not a problem, they can just resize right? Well, ideally yes, but
practically no. There are several places where my userControl is in use
on a fixed SplitContainer below other form elements that need to remain
visible all of the time.

I tried using a DataGridViewTextBoxCell, but it apparently has no
options for scrolling. I could probably make another usercontrol just
for that cell and just add a scrollbar on it as necessary, but I figured
that someone here must have run into this before and already solved it.

I've googled various terms, and the best items I can come up with don't
solve this particular issue, the relevant ones appear to be related to
just getting the DataGridView to do multi-line in the first place.

Example code is below. Yes, the form is sized that way intentionally to
demonstrate what is happening on the fixed SplitContainers.

Chris.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProofOfConcept
{
public class Form1 : Form
{
public DataTable DataSource
{
get { return this.dataGridView1.DataSource as DataTable; }
set
{
this.dataGridView1.DataSource = value;

DataGridViewColumnCollection col =
this.dataGridView1.Columns;
if (col.Contains("reallyLongComment"))
{
col["reallyLongComment"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
col["reallyLongComment"].DefaultCellStyle.WrapMode
= DataGridViewTriState.True;
}
}
}

public Form1()
{
InitializeComponent();

this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.AllCells;

DataTable dt = new DataTable("Comments");
dt.Columns.Add("reallyLongComment");
dt.Rows.Add("This is a short comment to demonstrate an
issue.");
dt.Rows.Add("This is another simple comment to demonstrate
the issue.");
dt.Rows.Add("This is a stupendously long comment that will
likely live on in the annals of " +
"history as one of the greatest comments to ever live.
Probably not, but I just need " +
"filler text to demonstrate that this is a problem.
Proving my concept and all.\n\n" +
"I've tossed in some line breaks so that it will cause
further spacing in the " +
"scrolling.\n\nThis should be enough.");
this.DataSource = dt;
}

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(560, 76);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(560, 76);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
}
}
 
C

Chris Shepherd

Okay, so, I've temporarily worked around it by doing the following:

- Disabled the scrollbars on the DataGridView.
- Set AutoScroll to true on my UserControl.
- Reset the DataGridView Dock property to None (it was Fill).
- Handled the UserControl's Paint event inside itself:

private void myUserControl_Paint(object sender, PaintEventArgs e)
{
this.SuspendLayout();

this.dataGridView1.Width = this.DisplayRectangle.Width;

if (this.dataGridView1.PreferredSize.Height <
this.DisplayRectangle.Height)
{
this.dataGridView1.Height = this.DisplayRectangle.Height;
}
else
{
this.dataGridView1.Height =
this.dataGridView1.PreferredSize.Height;
this.dataGridView1.Width = this.DisplayRectangle.Width -
SystemInformation.VerticalScrollBarWidth;
}

this.ResumeLayout();
}

I still think the scrollbar behaviour on the DataGridView is kind of
odd, and maybe needs to be looked at.

Anyone else's thoughts?

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top