代码如下: private void BindArrayListToGrid() { dataGrid1.DataSource = arrayList1; //create a custom tablestyle and add two columnstyles DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = "ArrayList"; //不明白的地方 int colwidth = (dataGrid1.ClientSize.Width - ts.RowHeaderWidth - SystemInformation.VerticalScrollBarWidth - 5) / 2; //create a column for the value property DataGridTextBoxColumn cs = new DataGridTextBoxColumn(); cs.MappingName = "value"; //public property name cs.HeaderText = "Random Number"; cs.Format = "f4"; cs.Width = colwidth; ts.GridColumnStyles.Add(cs); //create a column for the sqrt property cs = new DataGridTextBoxColumn(); cs.MappingName = "sqrt"; //public property name cs.HeaderText = "Square Root"; cs.Format = "f4"; cs.Width = colwidth; ts.GridColumnStyles.Add(cs); dataGrid1.TableStyles.Clear(); dataGrid1.TableStyles.Add(ts); } private void CreateArrayList() { arrayList1 = new ArrayList(); //add some items Random r = new Random(); for (int i = 0; i < 20; ++i) arrayList1.Add(new RandomNumber(r.NextDouble())); } //create a struct or class that defines what you want in each row //the different columns in the row must be public properties public struct RandomNumber { private double number; public RandomNumber(double d) { number = d; } public double value { get{ return number; } set{ number = value;} } public double sqrt { get {return Math.Sqrt(this.value);} } } }这个地方,BindArrayListToGrid函数的第5行 ts.MappingName = "ArrayList"; 为什么是"ArrayList"
ts.MappingName无论赋为什么值,甚至把这句去掉,也可显示结果
to 楼下:你还不明白我的问题,现在是ArrayList有了,用一楼提供的代码试了一下,不行行数是可以显示的,内容却一个也没有,通过调试,看到的ArrayList 也是正确的
{
dataGrid1.DataSource = arrayList1; //create a custom tablestyle and add two columnstyles
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "ArrayList"; //不明白的地方
int colwidth = (dataGrid1.ClientSize.Width - ts.RowHeaderWidth - SystemInformation.VerticalScrollBarWidth - 5) / 2;
//create a column for the value property
DataGridTextBoxColumn cs = new DataGridTextBoxColumn();
cs.MappingName = "value"; //public property name
cs.HeaderText = "Random Number";
cs.Format = "f4";
cs.Width = colwidth;
ts.GridColumnStyles.Add(cs);
//create a column for the sqrt property
cs = new DataGridTextBoxColumn();
cs.MappingName = "sqrt"; //public property name
cs.HeaderText = "Square Root";
cs.Format = "f4";
cs.Width = colwidth;
ts.GridColumnStyles.Add(cs);
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(ts);
} private void CreateArrayList()
{
arrayList1 = new ArrayList();
//add some items Random r = new Random();
for (int i = 0; i < 20; ++i)
arrayList1.Add(new RandomNumber(r.NextDouble()));
} //create a struct or class that defines what you want in each row //the different columns in the row must be public properties
public struct RandomNumber
{
private double number; public RandomNumber(double d)
{
number = d;
} public double value
{
get{ return number; }
set{ number = value;}
} public double sqrt
{
get {return Math.Sqrt(this.value);}
}
}
}这个地方,BindArrayListToGrid函数的第5行
ts.MappingName = "ArrayList"; 为什么是"ArrayList"