简体   繁体   English

在Datagridview中更改特定的行和列

[英]Change specific row and column in Datagridview

I want to change specific row and column in Datagridview. 我想更改Datagridview中的特定行和列。 when I enter specific data in column x it will return any specific value in column Y . 当我在x列中输入特定数据时,它将在Y列中返回任何特定值。 Otherwise data in column x will return value 7777 in column Y 否则, x列中的数据将在Y列中返回值7777

在此处输入图片说明

But when I run this code, there were nothing change in column Y : 但是,当我运行此代码时, 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

Then, I try to change this code, but return some same value at column Y : 然后,我尝试更改此代码,但在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

Thanks for the help! 谢谢您的帮助!

If the DataGridView is databound, you need modify the databound object not the grid directly. 如果DataGridView是数据绑定的,则需要修改数据绑定的对象,而不是直接修改网格。 You can access that object through the DataBoundItem property: 您可以通过DataBoundItem属性访问该对象:

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

just set MyObject and MyProperty as appropriate 只要适当设置MyObject和MyProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新 datagridview 中的特定行和特定列 - How to update a specific row and a specific column in a datagridview 将vb.net中具有特定列和行的csv文件导入到datagridview中 - import csv file into datagridview with specific column and row in vb.net 将组合框选择的值绑定到当前行的特定列(不是datagridview) - Binding combobox selected value to a specific column of current row (not a datagridview) DataGridView多行选择,特定列数据获取 - DataGridView multiple row selection, specific column data get 如果字段中的文本为“否”,则更改datagridview中特定行的颜色 - change colour of specific row in datagridview if text in field says “No” Select ComboBox 中的特定行,并在 DataGridView VB.NET 中同一行的其他列中添加金额 - Select a specific row from ComboBox and add amount in other Column from same row in DataGridView VB.NET Datagridview复选框更改行颜色 - Datagridview Checkbox Change Row Color DataGridView更改光标选择的行 - DataGridView change cursor selected row 如何在vb.net中的datagridview中的特定列中计算Row值的总和 - How to calculate the sum of Row values in a specific column in a datagridview in vb.net 仅在特定列中获取 Datagridview 行的总数(仅具有相同的列名) - Get The Total of Datagridview Row only in Specific Columns (Only with same Column Name)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM