2017-08-16 70 views
0

Datagridview中有3行。我在runnig應用程序之後看到了。爲什麼RowsAdded不是返回所有行?

但是當我使用RowsAdded事件時,它在控制檯中只顯示0,1。應該是0,1,2,爲什麼?

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) 
{ 
    Console.WriteLine(e.RowIndex); 
} 

我用dataGridView1_RowsAdded修改單元格數據。它適用於除外最後一行的所有行:

enter image description here

你可以看到,前兩個行被修改(日期)。最後不是

最少例如:

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) 
{ 
    Console.WriteLine("ok"); 

    string birthday = dataGridView1.Rows[e.RowIndex] 
     .Cells["dataGridViewTextBoxColumn42"].Value.ToString(); 
    DateTime dt = DateTime 
     .ParseExact(birthday, "dd/mm/yyyy", CultureInfo.InvariantCulture); 

    birthday = dt.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture); 
    dataGridView1.Rows[e.RowIndex] 
     .Cells["dataGridViewTextBoxColumn42"].Value = birthday; 
} 

回答

0

如果Datagridview屬性AllowUserToAddRows設置爲true,最後一行是插入和作爲@Fruchtzwerg提到

+0

這是假的,真的 – OPV

+0

是的,你可以試試。 –

+0

這不是理由。物業'AllowUserToAddRows'已經在假 – OPV

1

的行爲在MSDN記載:當行被添加到一個DataGridView控制發生

的RowsAdded事件。當用戶使用新記錄的行添加新行時,此事件的處理程序中的RowIndex值等於新記錄的行的新位置的索引,這比新剛剛添加的行大1。但是,以編程方式添加行時,RowIndex值是添加的第一行的索引。

RowIndex

獲取第一添加的行的索引。

請注意,最後一行是插入新數據。這意味着在用戶插入有效數據之前,此行不會添加到源代碼中。此時會彈出一個插入數據的新行。

+0

好,然後在那裏我可以重新插入數據? – OPV

+0

看看https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/specify-default-values-for-new-rows-in-the-datagrid – Fruchtzwerg

+0

我還沒找到什麼通過鏈接 – OPV

相關問題