2014-10-20 25 views
0

因此,在對StackOverFlow執行一些搜索之後,我發現下面只是以編程方式添加列。以編程方式在DataGridiew中在其他列之間創建列

private void AddColumnsProgrammatically() 
{ 
    // I created these columns at function scope but if you want to access 
    // easily from other parts of your class, just move them to class scope. 
    // E.g. Declare them outside of the function... 
    var col3 = new DataGridViewTextBoxColumn(); 
    var col4 = new DataGridViewCheckBoxColumn(); 

    col3.HeaderText = "Column3"; 
    col3.Name = "Column3"; 

    col4.HeaderText = "Column4"; 
    col4.Name = "Column4"; 

    dataGridView1.Columns.AddRange(new DataGridViewColumn[] {col3,col4}); 
} 

現在我沒有問題,創建列,但我是後加入這些新列其間可以說,第1列和第2列,所以列2將轉移到現在是4列和2個新列將是第2列和第3列。

有沒有人有任何建議可以給我或方向?

回答

0

您正在尋找DataGridView.Columns.Insert方法在特定索引中插入一列:

private void AddColumnsProgrammatically() 
{ 
    // I created these columns at function scope but if you want to access 
    // easily from other parts of your class, just move them to class scope. 
    // E.g. Declare them outside of the function... 
    var col3 = new DataGridViewTextBoxColumn(); 
    var col4 = new DataGridViewCheckBoxColumn(); 

    col3.HeaderText = "Column3"; 
    col3.Name = "Column3"; 

    col4.HeaderText = "Column4"; 
    col4.Name = "Column4"; 

    // dataGridView1.Columns.AddRange(new DataGridViewColumn[] {col3,col4}); 
    dataGridView1.Columns.Insert(1, col3); 
    dataGridView1.Columns.Insert(2, col4); 

}

相關問題