如何根据另一行设置datagridview行的值 [英] How do I set value of datagridview row based on another row

查看:142
本文介绍了如何根据另一行设置datagridview行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:

我在datagridview行中遇到此问题,其中我有3列。 Datagridview数据是从vb.net中的查询加载的。以下是场景:



Column1 Column2 Column3

3 75 3

3 76 3

3 IP 0

3 NFE 0



如果Column2单元格包含IP或NFE,Column3 值为0但是

如果Column2单元格包含75或76,则Column3值为EQUAL至Column1



我得到了这个代码,其中我使用了For Each循环;



但是很遗憾地注意到了这个错误让我回答;



从字符串IP转换为'Double'类型无效。



请帮我这个问题谢谢。



I had this problem in a datagridview row where i have 3 columns. Datagridview data is loaded from query in vb.net. Below is the scenario:

Column1 Column2 Column3
3 75 3
3 76 3
3 IP 0
3 NFE 0

If "Column2" cells contains "IP" or "NFE", "Column3" value is "0" BUT
If "Column2" cells contains "75" or "76", "Column3" value is "EQUAL" to "Column1"

I got this code where i used "For Each" loop;

BUT sad to note that returns me this ERROR;

"Conversion from string "IP" to type 'Double' is not valid."

Please help me with this problem thank you.





我的尝试:





What I have tried:

Private Sub DGVGRADES_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGVGRADES.CellValueChanged
        For Each row As DataGridViewRow In DGVGRADES.Rows
            If row.Cells(4).Value >= 75 Then
                row.Cells(0).Value = row.Cells(3).Value
            ElseIf row.Cells(4).Value.ToString <= 3 Then
                row.Cells(0).Value = row.Cells(3).Value
            ElseIf row.Cells(4).Value < 75 Then
                row.Cells(0).Value = 0
            ElseIf row.Cells(4).Value = "IP" Then
                row.Cells(0).Value = 0
            End If
        Next
    End Sub

推荐答案

错误信息很明显......这意味着您的 DataGridViewColumn [ ^ ]只接受数字数据,但源数据包含文本数据。

Yous有2个选项:

An error message is quite obvious... This means that your DataGridViewColumn[^] accepts only numeric data, but your source data contains text data.
Yous have got 2 options:


  1. 将数据表更改为linq查询,该查询将返回正确的数据类型

    请参阅: LINQ to DataSet中的查询| Microsoft Docs [ ^ ]



  2. 使用不同的DataGridView事件来处理非数字数据

    请参阅: DataGridView.RowPrePaint事件 [ ^ ]


  1. change datatable into linq query which will return proper data types
    See: Queries in LINQ to DataSet | Microsoft Docs[^]
  2. or
  3. use different DataGridView event to be able to handle non-numeric data
    See: DataGridView.RowPrePaint Event[^]


这篇关于如何根据另一行设置datagridview行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆