【问题标题】:DataMember Error when firing CellValueChanged触发 CellValueChanged 时出现 DataMember 错误
【发布时间】:2018-08-09 14:49:10
【问题描述】:

按照我前面的question 关于根据comboboxcolumncells 更改列单元格上的值,现在我遇到了以下问题。我的事件触发,但我在DataBindings 行收到以下错误消息:无法链接数据源中的属性或函数“值”。参数名称:dataMember。除此之外,其他列的值没有改变。在这种情况下我该怎么办?

   Private Sub dgv_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged

        Dim r As Integer = e.RowIndex
        Dim c As Integer = e.ColumnIndex

        Try
            If r > -1 Then 
                If c = 15 Then
                    dgv.DataBindings.Add("Text", dt, "value", False, DataSourceUpdateMode.OnPropertyChanged) 'I wanted to overwrite cells with the value associated with the code of the comboboxcell

                    Dim col_div_cell_value As Object = dt.Tables(0).Columns("value")

                    dgv.CurrentRow.Cells("col_1").Value = col_div_cell_value.Value()
                    dgv.CurrentRow.Cells("col_2").Value = (col_div_cell_value * col_3)

                End If
            End If

        Catch ex As Exception
            MsgBox("ERROR: " & ex.Message, MsgBoxStyle.Information)
        End Try

    End Sub

dt 的结构:这个datatable 被记入comboboxcell,它显示"code" 和我想在该行的其他列单元格上写入的值(Col_1 和 Col_2 ),是与该代码关联的"value"

code  |  date | value
---------------------
A       12/06   100
B       12/06   200
...

提前致谢

【问题讨论】:

    标签: .net vb.net data-binding datatable datagridviewcomboboxcell


    【解决方案1】:

    最后我不需要使用DataBindings。相反,我只为col_2col_3 列的字段赋值。这比我想象的要简单:

    Private Sub dgv_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgvBlotter.CellValueChanged
        Dim r As Integer = e.RowIndex
        Dim c As Integer = e.ColumnIndex
    
        Try
            If r > -1 Then 
                If c = 15 Then
    
                    Dim flag As Double = dgv.CurrentRow.Cells("col2").Value 'name of comboboxcolumn
                    Dim nuevovalor As Object = flag
                    Dim nominal As Double = dgv.CurrentRow.Cells("col_3").Value
    
                    dgv.CurrentRow.Cells("col_1").Value() = nuevovalor
                    dgv.CurrentRow.Cells("col_2").Value() = (nuevovalor * nominal)
    
                End If
            End If
        Catch ex As Exception
            MsgBox("ERROR: " & ex.Message, MsgBoxStyle.Information)
        End Try
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      澄清你的dt是什么? 在这里您声明其中有一个“价值”属性

      dgv.DataBindings.Add("Text", dt, "value", False, DataSourceUpdateMode.OnPropertyChanged)
      

      在这里您声明有一个 Tables 属性,并且在“0”-index 表内有一个“值”。

      Dim col_div_cell_value As Object = dt.Tables(0).Columns("value")
      

      我认为你应该使用 dt.Tables(0) 或类似的东西作为数据源

      【讨论】:

      • 我的 dt 是一个具有上述结构的数据表。我想(即时)获取与代码关联的值(在组合框中选择)。该值是该数据表的一列。对不起我的英语不好
      猜你喜欢
      • 2020-11-05
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2016-04-22
      • 2015-07-09
      • 2012-07-27
      • 1970-01-01
      相关资源
      最近更新 更多