【问题标题】:Change specific row and column in Datagridview更改 Datagridview 中的特定行和列
【发布时间】:2023-03-14 10:10:02
【问题描述】:

我想更改 Datagridview 中的特定行和列。 当我在x 列中输入特定数据时,它将在Y 列中返回任何特定值。否则x列中的数据将在Y列中返回值7777

但是当我运行这段代码时,Y 列没有任何变化:

Private Sub Table1DataGridView_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellEndEdit

    'For e.ColumnIndex + 1
    If e.ColumnIndex = 2 Then
        If Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 5

        ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 7 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7

        ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 1 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6
            '1
        ElseIf Val(Table1DataGridView.Rows(1).Cells(2).Value) = 1.5 Then
            Table1DataGridView.Rows(1).Cells(e.ColumnIndex + 1).Value = 7.326
            '2
        ElseIf Val(Table1DataGridView.Rows(2).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(2).Cells(e.ColumnIndex + 1).Value = 6.374
            '3
        ElseIf Val(Table1DataGridView.Rows(3).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(3).Cells(e.ColumnIndex + 1).Value = 4.702
            '4
        ElseIf Val(Table1DataGridView.Rows(4).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(4).Cells(e.ColumnIndex + 1).Value = 1.321
            '5
        ElseIf Val(Table1DataGridView.Rows(5).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(5).Cells(e.ColumnIndex + 1).Value = 0.035
            '6
        ElseIf Val(Table1DataGridView.Rows(6).Cells(2).Value) = 4 Then
            Table1DataGridView.Rows(6).Cells(e.ColumnIndex + 1).Value = 0.205
            '7
        ElseIf Val(Table1DataGridView.Rows(7).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(7).Cells(e.ColumnIndex + 1).Value = 4.969
            '8
        ElseIf Val(Table1DataGridView.Rows(16).Cells(2).Value) = 4 Then
            Table1DataGridView.Rows(16).Cells(e.ColumnIndex + 1).Value = 1.588
            '9
        ElseIf Val(Table1DataGridView.Rows(17).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(17).Cells(e.ColumnIndex + 1).Value = 4.696
            '10
        ElseIf Val(Table1DataGridView.Rows(18).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(18).Cells(e.ColumnIndex + 1).Value = 0.866
            '11
        ElseIf Val(Table1DataGridView.Rows(19).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(19).Cells(e.ColumnIndex + 1).Value = 1.881
            '12
        ElseIf Val(Table1DataGridView.Rows(20).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(20).Cells(e.ColumnIndex + 1).Value = 4.702
            '13
        ElseIf Val(Table1DataGridView.Rows(21).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(21).Cells(e.ColumnIndex + 1).Value = 6.515
            '14
        ElseIf Val(Table1DataGridView.Rows(22).Cells(2).Value) = 1.5 Then
            Table1DataGridView.Rows(22).Cells(e.ColumnIndex + 1).Value = 6.92671744
        Else
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7777

        End If
    End If

End Sub

然后,我尝试更改此代码,但在 Y 列返回一些相同的值:

Private Sub Table1DataGridView_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellEndEdit

    'For e.ColumnIndex + 1
    If e.ColumnIndex = 2 Then
        If Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 5

        ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 7 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7

        ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 1 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6
            '1
        ElseIf Val(Table1DataGridView.Rows(1).Cells(2).Value) = 1.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7.326
            '2
        ElseIf Val(Table1DataGridView.Rows(2).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6.374
            '3
        ElseIf Val(Table1DataGridView.Rows(3).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.702
            '4
        ElseIf Val(Table1DataGridView.Rows(4).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 1.321
            '5
        ElseIf Val(Table1DataGridView.Rows(5).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 0.035
            '6
        ElseIf Val(Table1DataGridView.Rows(6).Cells(2).Value) = 4 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 0.205
            '7
        ElseIf Val(Table1DataGridView.Rows(7).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.969
            '8
        ElseIf Val(Table1DataGridView.Rows(16).Cells(2).Value) = 4 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 1.588
            '9
        ElseIf Val(Table1DataGridView.Rows(17).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.696
            '10
        ElseIf Val(Table1DataGridView.Rows(18).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 0.866
            '11
        ElseIf Val(Table1DataGridView.Rows(19).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 1.881
            '12
        ElseIf Val(Table1DataGridView.Rows(20).Cells(2).Value) = 2.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.702
            '13
        ElseIf Val(Table1DataGridView.Rows(21).Cells(2).Value) = 2 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6.515
            '14
        ElseIf Val(Table1DataGridView.Rows(22).Cells(2).Value) = 1.5 Then
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6.92671744
        Else
            Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7777

        End If
    End If

End Sub

感谢您的帮助!

【问题讨论】:

    标签: vb.net winforms visual-studio-2010 datagridview


    【解决方案1】:

    如果DataGridView是数据绑定的,你需要直接修改数据绑定对象而不是网格。您可以通过 DataBoundItem 属性访问该对象:

    Dim obj As MyObject = Table1DataGridView.CurrentRow.DataBoundItem
    obj.MyProperty = newValue
    

    只需适当设置 MyObject 和 MyProperty

    【讨论】:

    • MyObject 出现错误。 MyObject 未声明。我可以在第 1 行这样做吗:Dim obj As MyObject = Table1DataGridView.1.DataBoundItem obj.MyProperty = 7.326 对不起我的基本水平。你能举个例子吗?
    • 您需要将 MyObject 和 MyPropertty 更改为您的网格绑定的任何内容。你是如何填充你的网格的?
    • 我正在使用 Windows.form.datagridview 填充网格。我从数据源中拖出 Table1。
    • 无论你的控件绑定什么,你都需要去改变数据的来源而不是控件本身。
    • MyObject 和 MyProperty 指的是我的行和列?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2021-03-29
    • 2021-08-05
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多