在datagridview中添加两行

在datagridview中添加两行

本文介绍了在datagridview中添加两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加datagridview的coman值,我的代码是这样的:但是它要求出现错误,以便我这么认为

i want to add the coman value of datagridview and my code is this : but it ask that a error occour so pleace hlp me

Private Sub calculateIngradient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim a, b As String
       For i = 0 To DataGridView1.RowCount - 1
           ''  a = DataGridView1.Rows(i).Cells(0).Value
           For j = 0 To DataGridView1.RowCount - 1
               ''  b = DataGridView1.Rows(j).Cells(0).Value
               '' If a = b Then

               DataGridView1.Rows(i).Cells(1).Value = DataGridView1.Rows(i).Cells(1).Value + DataGridView1.Rows(j).Cells(1).Value
               DataGridView1.Rows(i).Cells(2).Value = DataGridView1.Rows(i).Cells(2).Value & " / " & DataGridView1.Rows(j).Cells(2).Value
               DataGridView1.Rows.Remove(DataGridView1.Rows(j))

               ''End If
           Next
       Next
   End Sub

推荐答案

dim FirstSubTotalValue as integer = 0

For Each DatagridviewRow row in DataGridView1.Rows
 FirstValue += row.Cells(1).Value
Next

Messagebox.Show("Total : " & FirstValue.ToString())





我会做类似的事情来添加除了消息之外的现有网格的值框。然后创建新行并删除for循环外部所需的行,因为它将改变for循环正在使用的行集合并导致错误。



I would do something similar to this to add the values up of the existing grid apart from the message box. then create the new row and delete the rows required outside of the for loop as it will alter the collection of rows that the for loop is using and cause you an error.


For i = 0 To DataGridView1.RowCount-1
    DataGridView1.Rows(i).Cells(3).Value = DataGridView1.Rows(i).Cells(1).Value + DataGridView1.Rows(i).Cells(2).Value
    DataGridView1.Rows(i).Cells(4).Value = DataGridView1.Rows(i).Cells(3).Value / DataGridView1.Rows(i).Cells(1).Value
Next i



但记住,这不是好习惯!



请提供更多细节ls关于用于获取数据并在表单中显示它的方法(DataGridView)。然后我会尽力帮助你。


But remeber, this is not good practice!

Please, provide more details about methods you are using to fetch data and to display it in your form (DataGridView). Then i''ll try to help you.


这篇关于在datagridview中添加两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:06