2016-08-19 135 views
-1

當我嘗試更改列標題上的文本時,收到「索引超出範圍」錯誤。請參閱下面的代碼:[錯誤] [DataGridView]索引超出範圍

private void cboMonth_TextChanged(object sender, EventArgs e) 
{ 
    dgvAnnual.DataSource = null; 
    dgvAnnual.Rows.Clear(); 
    dgvAnnual.Columns.Clear(); 

    BindingSource bSource = new BindingSource(); 
    bSource.DataSource = ReloadData(dtTable, cboMonth.Text); 
    dgvAnnual.DataSource = bSource; 

    //Testing Code 
    Console.WriteLine(dgvAnnual.Columns.Count); //Col = 4 
    Console.WriteLine(dgvAnnual.Rows.Count); //Row = 10 

    //The Code below is Working 
    string[] str = {"Col1","Col2","Col3","Col4"}; 
    For(int i = 0;i<dvgAnnual.Columns.Count;i++) 
    { 
     dvgAnnual.Columns[i].HeaderText = str[i]; //<----- It's ok if it's stay in Loop For, event if I replace i by "0" 
    } 

    //The Code below not working if it stay outside For 
    dgvAnnual.Columns[0].HeaderText = "Col1"; //<----- Error if stay alone like this 
} 
+0

For(int i = 0; i <= dvgAnnual.Columns.count; i ++)應該是'<'!當然是'計數'.. – TaW

+2

您的代碼至少有2個錯誤,不會編譯或使其崩潰。請發佈[mcve]。 – nvoigt

+0

@Taw我的失敗了,我的代碼是正確的 –

回答

-1

列的值是4,但在循環中,你有它的範圍它i = 0;我< = 4意味着總數是5.所以改變你的循環範圍。使用我小於dvgAnnual.Columns.count而不是i小於等於dvgAnnual.Columns.count。去嘗試一下。

for(int i = 0;i<dvgAnnual.Columns.count;i++) 
    { 
     dvgAnnual.Columns[i].HeaderText = str[i]; //<----- It's ok if it's stay in Loop For, event if I replace i by "0" 
    }  
+0

所以你的語法不正確對於for循環,它是小寫 'for(int i = 0; i MethodMan

+0

@MethodMan哎呀!.. –

+0

我糾正了我的代碼。這是我複製並粘貼時未能再次檢查的失敗。抱歉。問題是行dgvAnnual.Columns [0] .HeaderText =「Col Name Here」。 –

0

工作代碼:

private void cboMonth_TextChanged(object sender, EventArgs e) 
    { 
     dgvAnnual.DataSource = null; 
     dgvAnnual.Rows.Clear(); 
     dgvAnnual.Columns.Clear(); 

     BindingSource bSource = new BindingSource(); 
     bSource.DataSource = ReloadData(dtTable, cboMonth.Text); 
     dgvAnnual.DataSource = bSource; 

     //Using for testing 
     Console.WriteLine(dgvAnnual.Columns.Count); //Col = 4 
     Console.WriteLine(dgvAnnual.Rows.Count); //Row = 10 

     //This Code below Working 
     string[] str = {"Col1","Col2","Col3","Col4"}; 
     For(int i = 0;i<dvgAnnual.Columns.Count;i++) 
     { 
      dvgAnnual.Columns[i].HeaderText = str[i]; //<----- It's ok if it's stay in Loop For, event if I replace i by "0" 
     } 
    } 

不工作:

private void cboMonth_TextChanged(object sender, EventArgs e) 
    { 
     dgvAnnual.DataSource = null; 
     dgvAnnual.Rows.Clear(); 
     dgvAnnual.Columns.Clear(); 

     BindingSource bSource = new BindingSource(); 
     bSource.DataSource = ReloadData(dtTable, cboMonth.Text); 
     dgvAnnual.DataSource = bSource; 

     //Testing Code 
     Console.WriteLine(dgvAnnual.Columns.Count); //Col = 4 
     Console.WriteLine(dgvAnnual.Rows.Count); //Row = 10 

     //This code below not working 
     dgvAnnual.Columns[0].HeaderText = "Col " + 1; //<----- Error: Index out of Range 
    } 

錯誤捕獲的監視1

+ dgvAnnual.Columns[0] 'dgvAnnual.Columns[0]' threw an exception of type 'System.ArgumentOutOfRangeException' System.Windows.Forms.DataGridViewColumn {System.ArgumentOutOfRangeException} 

感謝