当DataGridView
大于DataGridView本身时,我遇到了DataGridViewCell
控件(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
知道如何解决这个问题吗?
答案 0 :(得分:1)
我会截断超出一定大小的任何单元格内容(使用省略号表示截断)并允许单击该单元格以显示弹出窗口,其中全部内容在可滚动窗口中可见。或者我会在自定义UserControl中呈现这些可能很大的单元格的内容,如果文本超出一定长度,它本身就包含滚动条。
您遇到的问题是由于DataGridView以非预期的方式使用而导致的,所以我并不感到惊讶,没有简单的内置方法可以解决这个问题。
更新:要查看日志,ReportViewer
可能是更合适的控件。以下是一些使用它的链接: