[英]Set Datagridview to only display 50 rows that are pasted from excel
I have a vb.net program that has a datagridview and a button where end users are to paste data from excel to the grid using the button.我有一个 vb.net 程序,它有一个数据网格视图和一个按钮,最终用户可以使用按钮将数据从 excel 粘贴到网格中。 The user copies data from excel and click the paste button in program to paste the data in datagridview.用户从excel复制数据,点击程序中的粘贴按钮,将数据粘贴到datagridview中。 I only want the user to be able to paste 50 records in the datagridview.我只希望用户能够在 datagridview 中粘贴 50 条记录。 The issue I am having is the end users can copy over 50 rows and paste in the grid.我遇到的问题是最终用户可以复制 50 多行并粘贴到网格中。 It will display a message box that "The max number of rows is 50" but the grid will have all the rows from the excel copy to display in the grid.它将显示一个消息框,提示“最大行数为 50”,但网格会将 excel 副本中的所有行显示在网格中。 How do I set the paste to show the first 50 rows and remove the others how do I set the datagridview to only display 50 records?如何设置粘贴以显示前 50 行并删除其他行 如何将 datagridview 设置为仅显示 50 条记录?
Here is the copy I am using:这是我正在使用的副本:
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
I have tried to gridUserEntries.AllowUserToAddRows = False我试过 gridUserEntries.AllowUserToAddRows = False
I am expecting to be able to paste more than 50 record but only display the first 50 rows in the datagridview.我期望能够粘贴超过 50 条记录,但只显示 datagridview 中的前 50 行。
Change:改变:
For Each line As String In Clipboard.GetText.Split(vbNewLine)
To:到:
For Each line As String In Clipboard.GetText.Split(vbNewLine).Where(Function(s) Not String.IsNullOrWhiteSpace(s)).Select(Function(s) s.Trim()).Take(50)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.