我有一個 vb.net 程式,它有一個 datagridview 和一個按鈕,最終用戶可以使用該按鈕將資料從 excel 粘貼到網格中。用戶從 excel 中復制資料,然后單擊程式中的粘貼按鈕將資料粘貼到 datagridview 中。我只希望用戶能夠在 datagridview 中粘貼 50 條記錄。我遇到的問題是最終用戶可以復制超過 50 行并粘貼到網格中。它將顯示一個訊息框,“最大行數為 50”,但網格將包含 excel 副本中的所有行以顯示在網格中。如何將粘貼設定為顯示前 50 行并洗掉其他行 如何將 datagridview 設定為僅顯示 50 條記錄?
這是我正在使用的副本:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnPaste.Click
btnClear.Enabled = True
Dim maxRowCount As Integer
maxRowCount = 51
Try
For Each line As String In Clipboard.GetText.Split(vbNewLine)
If Not line.Trim.ToString = "" Then
Dim item() As String = line.Trim.Split(vbTab)
Me.gridUserEntries.Rows.Add(item)
End If
Next
If gridUserEntries.Rows.Count > maxRowCount Then
MsgBox("The max number of rows are 50")
'gridUserEntries.RowsRemoved()
'gridUserEntries.AllowUserToAddRows = False
' gridUserEntries.Rows.Remove(gridUserEntries.RowCount)
btnValidate.Enabled = False
Else
btnValidate.Enabled = True
btnRetrieve.Enabled = True
'gridUserEntries.Rows.Remove(row)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
我試過 gridUserEntries.AllowUserToAddRows = False
我希望能夠粘貼超過 50 條記錄,但只顯示 datagridview 中的前 50 行。
uj5u.com熱心網友回復:
改變:
For Each line As String In Clipboard.GetText.Split(vbNewLine)
到:
For Each line As String In Clipboard.GetText.Split(vbNewLine).Where(Function(s) Not String.IsNullOrWhiteSpace(s)).Select(Function(s) s.Trim()).Take(50)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453920.html
下一篇:如何檢查重量是否在兩個值之間
