我对象的列表绑定到一个DataGridView,并具有与显示问题: 这是我的自定义类上的DataGridView无法格式化列必将列出<T>
public class Line
{
private int _sequence;
public int Sequence {get { } set { }}
private string _dataTime;
public string DataTime {get { } set { }}
private string _content;
public string Content {get { } set { }}
public Line(int sequence, string dateTime, string content)
{
_sequence = sequence;
_dataTime = dateTime;
_content = content;
IsBookmarked = false;
}
}
我绑定的列表:
List<Line> lines = new List<Line>();
// lines is initialized with values
DataGridView dataGrid = new DataGridView();
dataGrid.DataSource = Lines
DataGridView显示列表的内容没有问题。然而列是很窄的,所以我想格式化列:
dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
在这里,我得到一个异常:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
检查在运行时dataGrid.Columns.Count我看这就是它“ 0',而不是我所期望的那样。
我认为你是绑定列表,并在运行时创建GridView的对象,否则它会工作完美?我的意思是将网格放置在窗体上,并且如果您在该网格的表单加载中写入自动调整代码。 – paragy 2011-01-05 19:47:51
我在运行时创建datagridview和绑定。我可以在运行时自己创建列并对它们进行格式化,但是然后我得到这些预制的列,并且新的列显示类的属性(所以我有6列而不是3列,3列是空的。) – Yoav 2011-01-06 00:33:42