2013-10-29 28 views
0

在包含datagridview的表單上以及具有不同部分號的drop-donwlist中,當我按下鍵盤上的刪除鍵時, dgv中的選定行被刪除,但第一行對dgv隱藏。我必須從下拉列表中手動重新選擇p/n才能再次查看。我在表單上也有一個刪除按鈕,但它完美地工作,並使用相同的子DeleteCurrentRow。這是在KeyDown處理程序鍵盤的Delete鍵:當我在datagridview的某一行上按下鍵盤刪除鍵時,兩行消失

Private Sub DeleteCurrentRow() 
    If Trim(CollectionPntPNList.Text).Length = 0 OrElse (CurrentPriorPassRequirementId = 0) Then 
     MessageBox.Show("Nothing to delete." & vbNewLine & vbNewLine & _ 
         "Please select a Part Number from the drop-down list, then select any cell on the data grid view." & _ 
         vbNewLine & vbNewLine & "When the Delete button is pressed, the highlighted row will be deleted.", 
         "Nothing to delete", 
         MessageBoxButtons.OK, 
         MessageBoxIcon.Information) 
     Exit Sub 
    End If 


    Dim result = MessageBox.Show("Delete the highlighted record? (Yes/No)", 
         "Ok to delete?", 
         MessageBoxButtons.YesNo, 
         MessageBoxIcon.Question) 
    If result = vbNo Then 
     Exit Sub 
    Else 
     CollectionPoints.sDeleteCollectionPointSet(CurrentPriorPassRequirementId) 
     DisplayCurrentCollectionPoints() 
     DGVCurrentPoints.Refresh() 
    End If 
End Sub 

:如果我按鍵盤上的Delete鍵,或者刪除按鈕的形式

Private Sub DGVCurrentPoints_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGVCurrentPoints.KeyDown 
    If e.KeyValue = Keys.Delete Then 
     DeleteCurrentRow() 
    End If 
End Sub 

此子,DeleteCurrentRow,被稱爲CollectionPoints是一個類,DisplayCurrentCollectionPoints()是用於重新填充dgv的本地/私有子。這裏是DisplayCurrentCollectionPoints():

Private Sub DisplayCurrentCollectionPoints() 
    Dim objConn As New SqlConnection(DatabaseConnection.FISSQLConnectionString) 
    Dim objCommand As SqlCommand = objConn.CreateCommand 
    Dim strSQL As New StringBuilder 
    DGVCurrentPoints.DataSource = Nothing 

    strSQL.Append("SELECT ") 
    strSQL.Append("  ppr.PriorPassRequirementId AS [Row ID], ") 
    strSQL.Append("  RTRIM(LTRIM(cp1.Description)) AS Description1, ") 
    strSQL.Append("  ppr.CollectionPointId AS Point1, ") 
    strSQL.Append("  RTrim(LTrim(cp2.Description)) AS Description2, ") 
    strSQL.Append("  ppr.ReqPassedCollectionPointId AS Point2, ") 
    strSQL.Append("ppr.Enabled, ") 
    strSQL.Append("cp1.CollectionStep AS Step1, ") 
    strSQL.Append("cp2.CollectionStep AS Step2 ") 
    strSQL.Append("FROM ") 
    strSQL.Append("  PriorPassRequirements AS ppr INNER JOIN ") 
    strSQL.Append("  CollectionPoints AS cp1 ON ppr.CollectionPointId = cp1.CollectionPointId INNER JOIN ") 
    strSQL.Append("  Products ON cp1.ProductIdValue = Products.ProductId INNER JOIN ") 
    strSQL.Append("  CollectionPoints AS cp2 ON Products.ProductId = cp2.ProductIdValue ") 
    strSQL.Append("   AND ppr.ReqPassedCollectionPointId = cp2.CollectionPointId ") 
    strSQL.Append("WHERE ") 
    strSQL.Append("  (ppr.CollectionPointId IN ") 
    strSQL.Append("    (SELECT  cp.CollectionPointId ") 
    strSQL.Append("    FROM ") 
    strSQL.Append("     CollectionPoints AS cp INNER JOIN ") 
    strSQL.Append("     Products AS p ON cp.ProductIdValue = p.ProductId ") 
    strSQL.Append("    WHERE ") 
    strSQL.Append("     (p.PartNumber = N'" & Trim(CollectionPntPNList.Text) & "'))) ") 
    strSQL.Append("ORDER BY Step1, Step2") 
    objCommand.CommandText = strSQL.ToString 

    Dim dtCurrPnts As New DataTable 
    objConn.Open() 
    dtCurrPnts.Load(objCommand.ExecuteReader) 
    objConn.Dispose() 
    With DGVCurrentPoints 
     .DataSource = dtCurrPnts 
     .AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill 
     .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter 
     .Columns("Row ID").Visible = True 
     .Columns("Description1").ReadOnly = True 
     .Columns("Point1").ReadOnly = True 
     .Columns("Description2").ReadOnly = True 
     .Columns("Point2").ReadOnly = True 
     .Columns("Enabled").ReadOnly = False 
    End With 
End Sub 

的DGVCurrentPoints_KeyDown活動結束後分,在DGV第一行被隱藏,但不會被刪除。這是我必須從下拉菜單中重新選擇p/n的地方。當我使用鍵盤刪除鍵時會導致第一行被隱藏?我使用dgv.Refresh(),但似乎沒有工作。


每從Plutonix的建議,我已經添加了這一分拿刪除的護理:

Private Sub DGVCurrentPoints_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGVCurrentPoints.KeyDown 
    If e.KeyValue = Keys.Delete Then 
     e.Handled = True 
     DeleteCurrentRow() 
     Exit Sub 
    End If 
End Sub 

回答

2

的DGV有已經可以響應刪除選定/當前行Delete鍵。因此,當用戶按下刪除時,DGV刪除一個,然後刪除一個。這也是按鈕按預期工作的原因。該AllowUserToDeleteRows將防止這種所以你可以做到這一切的代碼,或補充一點:從得到的按鍵

+0

其實我已經嘗試過這種方法減去DeleteCurrentRow()調用

If e.KeyValue = Keys.Delete Then e.Handled = True ' add DeleteCurrentRow() End If 

這可以防止DGV但它看起來並不像它的幫助,所以我把它留下了。我會再試一次,看看。此外,我已更新我的帖子以反映新添加的子文件。 – ctmcklowe96

+0

只需設置'AllowUserToDeleteRows = False',因爲您有運行代碼來檢查事物 – Plutonix

+0

我首先將它放在錯誤的子部分中,但現在它在keydown處理程序中並且效果很好。我的代碼的底部顯示缺少的布爾表達式。 – ctmcklowe96

相關問題