从dataGridView1的(表1)总结的值,并将其保存到Datagridview2(表2)使用LINQ和MySQL [英] sum the values from Datagridview1(Table1) and save it to Datagridview2(Table2) Using Linq and MySql

查看:192
本文介绍了从dataGridView1的(表1)总结的值,并将其保存到Datagridview2(表2)使用LINQ和MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 dataGridView1的(表1)值相加,并把它保存到 Datagridview2(表2)

两个 DGVs 数据绑定键,都具有相同的2列名称,数量

Childform1(DGV1)我有一个 Textbox1的发布按钮。

,用户必须提供一个名称 Textbox1的,点击发布,那么所有的 DGV1 必须概括起来,有相同的名称作为输入的 Textbox1的

例如:

表1

 名称|数量
 10
 B 5
 10
 B 5
 

在pressing POST 按钮

表2

 名称|数量
 20
 B 10
 

codeS

 昏暗的事件再度发生作为新词典(串,双人间)
    昏暗oTempObjects作为新的列表(中的DataGridViewRow)

    MasterForm.oTransferRows = oTempObjects

    对于每个xRow作为的DataGridViewRow在Datagridview1.Rows

        如果(不xRow.Cells(GVName)。值是没有AndAlso xRow.Cells(GVName)。Value.ToString()= TextBox1.Text)然后

            oTempObjects.Add(xRow)
        结束如果
    下一个

    昏暗_Name作为新的列表(串)

    对于每个xRows在MasterForm.oTransferRows

        _Name.Add('与& xRows.Cells(GVName)Value.ToString()及')


        昏暗inClause1作为字符串=的string.join(,_Name.ToArray())
        昏暗_sqlUpdate作为字符串=的String.Format(更新测试设置发布= @Posted WHERE名称({0}),inClause1)

        使用_conn作为新的MySqlConnection(服务器=本地主机;用户名=根;密码=;数据库=测试)
            使用_commm作为新的MySqlCommand()
                随着_commm

                    .CommandText = _sqlUpdate
                    ■连接= _conn
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue(@发布,是)

                结束与
                尝试

                    _conn.Open()
                    _commm.ExecuteNonQuery()

                抓住EX作为个MySqlException

                    MSGBOX(ex.StackTrace.ToString)

                结束尝试

                _conn.Close()

            结束使用
        结束使用

        如果(occurences.ContainsKey(xRows.Cells(1).Value.ToString()))然后

            事件再度发生(xRows.Cells(1).Value.ToString())= Double.Parse(点(xRows.Cells(1).Value.ToString())。的ToString())+ Double.Parse(xRows.Cells(4- ).Value.ToString())

        其他

            occurences.Add(xRows.Cells(1).Value.ToString(),Double.Parse(xRows.Cells(4).Value.ToString()))
        结束如果

    下一个

    对于每个KVP作为KeyValuePair(串,双)在事件再度发生
        oChildForm2.DataGridView2.Rows.Add(新的对象(){KVP.Key,KVP.Value})
    下一个

    昏暗的Xlist方式列表(中KeyValuePair(串,双人间))= occurences.ToList

    对于每一对作为KeyValuePair(串,双)在事件再度发生


        昏暗oUpdateString =的String.Format(插入测试(名称,数量)VALUES(@iName,@iQty))
        使用_conn作为新的MySqlConnection(服务器=本地主机;用户名=根;密码=;数据库=测试)
            使用_commm作为新的MySqlCommand()
                随着_commm

                    .CommandText = oUpdateString
                    ■连接= _conn
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue(@ INAME,pair.Key)
                    .Parameters.AddWithValue(@ iQty,pair.Value)
                结束与
                尝试

                    _conn.Open()
                    _commm.ExecuteNonQuery()

                抓住EX作为个MySqlException

                    MSGBOX(ex.StackTrace.ToString)

                结束尝试

                _conn.Close()

            结束使用
        结束使用
    下一个
 

这code只是总结上的 DGV1 相同名称的第一个点击并将其保存到 DGV2

但比如我上添加 DGV1 另一个数据具有相同的名称,

欲新添加的数据的量来添加到上DGV2现有数据的当前总和

一点点帮助,这也将是非常真棒。**

例如:

表2

 名称|数量
 20
 B 10
 

然后,添加表1的数据。

表1

 名称|数量
  20
  B 10
 

在pressing帖子

表2

 名称|数量
 40
 b 20分配
 

解决方案

通过你的行进行迭代时,看看是否行已被张贴或不检查张贴栏:

 对于每个xRows在MasterForm.oTransferRows.Where(功能(X)x.Cells(发布)。Value.ToString()=否)
 

然后用下面的伪逻辑来写你的code:

 'code选择数据 - 选择名字来自测试,其中name ='产品1'
 如果(dr.Read())
 更新到这里
 '其他
 插入到这里
 如果结束
 

I am trying to sum the values from Datagridview1(Table1) and save it to Datagridview2(Table2)

both DGVs are Databound and both have the same 2 columns Name, Quantity.

On Childform1(DGV1) I have a Textbox1 and a Post button.

The user must input a Name on Textbox1 and click Post, then all the Quantity on DGV1 must be summed up that has the same Name as entered on Textbox1.

ex:

Table1

Name | Quantity
 a       10
 b        5
 a       10
 b        5

After pressing POST button

Table 2

Name | Quantity
 a        20
 b        10

Codes

    Dim occurences As New Dictionary(Of String, Double)
    Dim oTempObjects As New List(Of DataGridViewRow)

    MasterForm.oTransferRows = oTempObjects

    For Each xRow As DataGridViewRow In Datagridview1.Rows

        If (Not xRow.Cells("GVName").Value Is Nothing AndAlso xRow.Cells("GVName").Value.ToString() = TextBox1.Text) Then

            oTempObjects.Add(xRow)
        End If
    Next

    Dim _Name As New List(Of String)

    For Each xRows In MasterForm.oTransferRows

        _Name.Add("'" & xRows.Cells("GVName").Value.ToString() & "'")


        Dim inClause1 As String = String.Join(",", _Name.ToArray())
        Dim _sqlUpdate As String = String.Format("UPDATE tested SET Posted = @Posted WHERE Name IN ({0})", inClause1)

        Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
            Using _commm As New MySqlCommand()
                With _commm

                    .CommandText = _sqlUpdate
                    .Connection = _conn
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@Posted", "Yes")

                End With
                Try

                    _conn.Open()
                    _commm.ExecuteNonQuery()

                Catch ex As MySqlException

                    MsgBox(ex.StackTrace.ToString)

                End Try

                _conn.Close()

            End Using
        End Using

        If (occurences.ContainsKey(xRows.Cells(1).Value.ToString())) Then

            occurences(xRows.Cells(1).Value.ToString()) = Double.Parse(occurences(xRows.Cells(1).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(4).Value.ToString())

        Else

            occurences.Add(xRows.Cells(1).Value.ToString(), Double.Parse(xRows.Cells(4).Value.ToString()))
        End If

    Next

    For Each KVP As KeyValuePair(Of String, Double) In occurences
        oChildForm2.DataGridView2.Rows.Add(New Object() {KVP.Key, KVP.Value})
    Next

    Dim xlist As List(Of KeyValuePair(Of String, Double)) = occurences.ToList

    For Each pair As KeyValuePair(Of String, Double) In occurences


        Dim oUpdateString = String.Format("Insert Into testing (Name, Quantity) VALUES (@iName, @iQty)")
        Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
            Using _commm As New MySqlCommand()
                With _commm

                    .CommandText = oUpdateString
                    .Connection = _conn
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@iName", pair.Key)
                    .Parameters.AddWithValue("@iQty", pair.Value)
                End With
                Try

                    _conn.Open()
                    _commm.ExecuteNonQuery()

                Catch ex As MySqlException

                    MsgBox(ex.StackTrace.ToString)

                End Try

                _conn.Close()

            End Using
        End Using
    Next

This code only sums the same Name on the DGV1 on the first click and saves it on DGV2.

but for example I add another data on DGV1 with the same name,

I want the QUANTITY of the newly added data to add to the current sum of the existing data on DGV2.

A little help with this too would be really awesome.**

Ex:

TABLE 2

Name | Quantity
 a       20
 b       10

Then I add data on Table 1

Table1

Name | Quantity
  a       20
  b       10

After Pressing Post

Table2

Name | Quantity
 a        40
 b        20

解决方案

Check the Posted column when iterating through your rows to see if a row has been posted or not:

For Each xRows In MasterForm.oTransferRows.Where(Function(x) x.Cells("Posted").Value.ToString() = "No")

Then use the following psuedo-logic to write your code:

 'code to select data - select name from testing where name = 'Product1'
 'if(dr.Read())
 'update goes here
 'Else
 ' insert goes here
 'End if

这篇关于从dataGridView1的(表1)总结的值,并将其保存到Datagridview2(表2)使用LINQ和MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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