DataGridView:CellEnter没有显示正确的RowCount

时间:2019-02-19 15:14:09

标签: vb.net datagridview

我创建了一个带有datagridview和标签的用户控件,用于显示行数。在使用控件的某些项目中,CellEnter事件出现了问题,该事件未显示正确的行数。我将label.text放置在CellEnter事件中,以便可以根据输入的单元格更改标签值。

在我的一个项目中,我有一些用作选项卡的按钮。加载表单时,它将添加新控件并填充数据。单击按钮时,它将删除现有控件并添加新控件(之所以这样做,是因为出于某种原因,即使我已经清除了数据表并将数据源设置为空,来自上一个表的列仍保留在dgv中)。对于我绑定到datagridview的某些表,CellEnter屏幕上的行数显示2(允许将添加行设置为true),其中与DataSourceChange或CellValidating中的一样,它显示的是正确的数字(例如664)。这只会在第一次加载时发生,一旦移动到其他单元格,它就会显示正确的值。

因此,这是第一次加载表单时的顺序:
1. CellEnter(行数= 2)
2. DataSourceChanged(行数= 664)
3. CellValidating(行数= 664)
4. CellEnter(行数= 2)

单击与第一次加载表单时加载相同表的按钮:
1. CellEnter(行数= 2)
2. DataSourceChanged(行数= 664)

我试图根据从DataSourceChange获得的值使用行数,但是如果删除或添加了记录,它将使标签值混乱。 还尝试将标签移动到CellValidating,但是它并不总是显示正确的行(例如:单击单元格664并移动到单元格659,标签仍显示664)。 如果将dgv1.dgvEditor.CurrentCell = dgv1.dgvEditor.Item("TAG_CODE", 1)添加到PopulateCodesGrid,它将显示正确的标签,但是当将行更改为0时,它将以1的1结尾。

Private Sub dgvEditor_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEditor.CellEnter
    If AllowUserToAddRow Then
        If dgvEditor.CurrentCell.RowIndex = dgvEditor.Rows(dgvEditor.NewRowIndex).Index Then
            NewRowIndex = dgvEditor.CurrentCell.RowIndex
            lblDataNav.Text = dgvEditor.CurrentCell.RowIndex + 1 & " of " & dgvEditor.RowCount
        ElseIf NewRowIndex <> -1 Then
            lblDataNav.Text = dgvEditor.CurrentCell.RowIndex + 1 & " of " & dgvEditor.RowCount - 1
        Else
            lblDataNav.Text = dgvEditor.CurrentCell.RowIndex + 1 & " of " & dgvEditor.RowCount - 1
        End If
    Else
        lblDataNav.Text = dgvEditor.CurrentCell.RowIndex + 1 & " of " & dgvEditor.RowCount
    End If
End Sub


Private Sub SystemAdminForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    dgv1 = New SQLTableEditor.ctlTableEditor
    Me.Controls.Add(dgv1)
    dgv1.BringToFront()

    dgv1.Visible = True
    dgv1.Location = New Point(25, 115)
    dgv1.Size = New Size(722, 550)
    dgv1.HeaderLabelVisible = False
    dgv1.DataNavigatorVisible = True

    PopulateCodesGrid()
End Sub

Private Sub CodesTab_Click(sender As System.Object, e As System.EventArgs) Handles CodesTab.Click
    'Button click
    If (dgv1 IsNot Nothing) AndAlso (Not dgv1.IsDisposed) Then dgv1.Dispose()

        Dim btn As Button = DirectCast(sender, Button)
        Select Case btn.Name
            Case "CodesTab"
                dgv1 = New SQLTableEditor.ctlTableEditor
                Me.Controls.Add(dgv1)
                dgv1.BringToFront()

                dgv1.Visible = True
                dgv1.Location = New Point(25, 115)
                dgv1.Size = New Size(722, 550)
                dgv1.HeaderLabelVisible = False
                dgv1.DataNavigatorVisible = True

                PopulateCodesGrid()
End Sub

Private Sub PopulateCodesGrid()
    Try

        Using connection As New SqlClient.SqlConnection(MAIN_CONN)
            connection.Open()
            Using dAdapt As New SqlClient.SqlDataAdapter("spGetCodes", connection)

                Dim colCategory = New DataGridViewComboBoxColumn
                colCategory.DataPropertyName = "CATEGORY"
                colCategory.HeaderText = "Category"
                colCategory.Name = "CATEGORY"
                colCategory.Width = 150
                colCategory.SortMode = DataGridViewColumnSortMode.Automatic
                For Each item In viewModel.DefectCategoryDropDownList
                    colCategory.Items.Add(item.Value)
                Next

                dgv1.dgvEditor.Columns.Add(colCategory)

                dgv1.AllowUserToAddRow = True
                dgv1.AllowUpdateEditRecord = True
                dgv1.AllowUserToDeleteRow = False
                dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells

                dAdapt.Fill(dgv1.dTable)
                dgv1.BindingSrc.DataSource = dgv1.dTable
                dgv1.dgvEditor.DataSource = dgv1.BindingSrc

                dgv1.dgvEditor.Columns("TAG_CODE").DisplayIndex = 0
                dgv1.dgvEditor.Columns("TAG_CODE").HeaderText = "CODE"
                dgv1.dgvEditor.Columns("TAG_CODE").ReadOnly = True
                dgv1.dgvEditor.Columns("PROBLEM").DisplayIndex = 1
                dgv1.dgvEditor.Columns("PROBLEM").HeaderText = "Description"
                dgv1.dgvEditor.Columns("CATEGORY").DisplayIndex = 2
                dgv1.dgvEditor.Columns("INACTIVE_DATE").DisplayIndex = 3
                dgv1.dgvEditor.Columns("INACTIVE_DATE").HeaderText = "Inactive Date"

                RemoveHandler dgv1.InsertRecord, AddressOf RTCodesInsertRecord
                RemoveHandler dgv1.UpdateRecord, AddressOf RTCodesUpdateRecord
                RemoveHandler dgv1.CellDefaultValue, AddressOf CellDefaultValue
                RemoveHandler dgv1.CellClick, AddressOf DGV1CellClickEvent

                AddHandler dgv1.InsertRecord, AddressOf RTCodesInsertRecord
                AddHandler dgv1.UpdateRecord, AddressOf RTCodesUpdateRecord
                AddHandler dgv1.CellDefaultValue, AddressOf CellDefaultValue
                AddHandler dgv1.CellClick, AddressOf DGV1CellClickEvent

            End Using
        End Using

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

我希望CellEnter中的行数为664,而不是2。我有其他程序员帮助我查看我的代码,但也无法弄清楚。请注意,这只发生在同一项目的某些其他表上就可以了。

0 个答案:

没有答案