C# Code
private void btnClone_Click(object sender, EventArgs e)
{
// 寫法一
List<DataGridViewColumn> cloneColumn = new List<DataGridViewColumn>();
foreach (DataGridViewColumn col in dataGridView1.Columns)
cloneColumn.Add(col.Clone() as DataGridViewColumn);
// 寫法二
List<DataGridViewColumn> cloneColumn = dataGridView1.Columns.OfType<DataGridViewColumn>()
.Select(col => col.Clone() as DataGridViewColumn)
.ToList();
dataGridView2.Columns.AddRange(cloneColumn.ToArray());
}
從中斷點內可以觀察到 DataGridViewColumn.DataGridView 屬性變成 null
實際執行
沒有留言:
張貼留言