代码之家  ›  专栏  ›  技术社区  ›  rostamiani

我的DataGridView的CellValueChanged事件中的RowIndex为-1

  •  0
  • rostamiani  · 技术社区  · 6 年前

    在显示到DataGridView之前,我需要更改一些数据,我正在使用CellValueChanged事件。问题是,当datagridview填充数据源时,e.RowIndex是-1。

    Private Sub dgvList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvList.CellValueChanged
    
        ' Convert date to Persian if the edited cell has date
            If dgvList.Rows(e.RowIndex).Cells.Item(e.ColumnIndex).ValueType.ToString = "" Then
                ' Do something
            End If
    
    End Sub
    

            Dim MyRow = dstList.Tables("list").NewRow
    
            MyRow.Item("date_reported") = PersianDateTime.Parse(txtDateReport.Text).ToDateTime
            MyRow.Item("description") = txtDescription.Text
            MyRow.Item("duration") = txtDuration.Text
            MyRow.Item("coworkers") = txtCoworkers.Text
            MyRow.Item("progress") = numProgress.Value
            MyRow.Item("problems") = txtProblems.Text
            MyRow.Item("date_created") = Now.Date
    
            ' Add this row to the dataset
            dstList.Tables("list").Rows.Add(MyRow)
    
            ' Apply changes to database
            UpdateList()
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   jmcilhinney    6 年前

    这很正常。我想是列标题设置好的时候。你只要加一个 If 语句来过滤掉它。

    If e.RowIndex <>  -1 Then
        'It's a valid cell.
    End If
    
        2
  •  0
  •   CruleD    6 年前
    If e.RowIndex > -1 AndAlso e.ColumnIndex > -1 Then
          Debug.Print(dgvList(e.ColumnIndex, e.RowIndex).ValueType.ToString)
          Debug.Print(dgvList(e.ColumnIndex, e.RowIndex).Value.ToString)
    End If
    

    对我来说很好。

    你是如何改变价值观的? 也张贴部分代码,改变它。因为变更源给出的是索引而不是结果(您发布的代码)。