2010-11-30 95 views
0

我正在嘗試將輸入焦點並將編輯事件觸發到添加到DataGridView窗體中的每個新行上。在DataGridView中選擇新行的第一個可見單元格

這是我正在嘗試用戶來實現此目的的代碼。

Private Sub grd_GoldAdders_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grd_GoldAdders.RowsAdded 
     Dim grid As DataGridView = DirectCast(sender, DataGridView) 

     grid.ClearSelection() 

     If grid.Rows(e.RowIndex).Cells("grid_flag").FormattedValue = Constants.[New] Then 

      For Each cell As DataGridViewCell In grid.Rows(e.RowIndex).Cells 
       If Not cell.Visible Then Continue For 
       grid.CurrentCell = cell 
       grid.BeginEdit(False) 
       Exit For 
      Next 

     End If 

    End Sub 

「grid_flag」是一個隱藏單元,用於存儲行的自定義狀態。

之前添加行,這就是我們在表單上看到: Before we add a new row.

這就是我們看到的,當我們真正嘗試,並添加一個新行: Row Add button clicked.

注意的是,列0,0和新行的第一個可見列被選中,但列0,0具有焦點。我不希望0,0得到選擇或有焦點。我在此還看到該行指標在0行太指着...

這是我想看到的東西我點擊添加按鈕後: Desired outcome from clicking the Add button.

有誰知道我錯了用代碼?我在一天中的大部分時間都在努力解決這個問題。

回答

0

而不是使用您的DataGridView的RowAdded事件設置CurrentCell的,添加以下代碼,無論你要添加一個新的記錄您的DGV(在添加按鈕的Click事件中,我假設):

''# Add the new record to your Data source/DGV. 

For Each row As DataGridViewRow In grd_GoldAdders.Rows 
    If row.Cells("grid_flag").FormattedValue = Constants.[New] Then 
     grd_GoldAdders.CurrentCell = row.Cells("AssySiteColumn") ''# I'm calling the first column in your DGV 'AssySiteColumn'. 
     grd_GoldAdders.BeginEdit(False) 
     Exit For 
    End If 
Next 

這代碼只是循環遍歷DGV中的所有行,並將您的Constants.[New]標誌值指定爲CurrentCell中第一行的第一個單元格。

+0

如果行被排序爲網格中最後一行(數字)可能不是最後一行(按時間順序)添加,這將無法正常工作。 – Mike 2010-11-30 19:17:45

相關問題