在VB.NET中对多个datagridviews中的重复值求和 [英] Sum repeated values in multiple datagridviews in VB.NET
问题描述
大家好,
我在tabcontrol中的表单上有多个datagridviews,每个都包含以下列:ref,Bestand zu KHK(HW) ),Bestand Menge和Bestandsart。
问题是ref包含重复值,我想要做的是删除所有重复的值并将相应的Bestand zu KHK(HW),Bestand Menge合并为一个。
我的尝试:
Dim i = t1 - 1
Do直到i = t2
Dim j = 0
Do until j = datagridviews(i).RowCount
如果不是datagridviews(i).Rows(j).Cells(ref ).Value.Equals(0)然后
Dim s1
Dim s2
s1 = datagridviews(i).Rows(j).Cells(Bestand zu KHK(HW) ))。值
s2 = datagridviews(i).Rows(j).Cells(Bestand Menge)。Value
For t = j + 1 To datagridviews(i).RowCount - 1
如果datagridviews(i).Rows(j).Cells(ref)。Value.Equals(datagridviews(i).Rows(t).Cells(ref) .Value)和datagridviews(i).Rows(j).Cells(Bestandsart)。Value.Equals(datagridviews(i).Rows(t).Cells(Bestandsart)。Value)然后
s1 = s1 + datagridviews(i).Rows(t).Cells(Bestand zu KHK(HW)。)
s2 = s2 + datagridviews(i).Rows(t).Cells(Bestand Menge) ).Value
datagridviews(i).Rows(t).Cells(ref)。Value = 0
datagridviews(i).Rows(t).Cells(Bestandsart) ).Value = 0
datagridviews(i).Rows(t).Cells(Bestand Menge)。Value = 0
datagridviews(i).Rows(t).Cells(Bestand zu KHK (HW))。值= 0
结束如果
下一个
datagridviews(i).Rows(j).Cells(Bestand zu KHK(HW))。值= s1
da tagridviews(i).Rows(j).Cells(Bestand Menge)。Value = s2
j = j + 1
ElseIf datagridviews(i).Rows(j).Cells(ref)。Value.Equals(0)然后
j = j + 1
结束如果
循环
BackgroundWorker1.ReportProgress((i - (t1 - 1)+ 1)/(t2 - t1 + 1)* 100)
i = i + 1
循环
一旦我点击按钮执行代码,我就不会收到错误但它返回部分总和意味着我仍然看到一些重复的值,我必须再按一次才能完成这项工作。
目前有21个datagridviews,每个包含大约1100排。
有人可以帮忙或指向正确的方向。
提前感谢。
您可以为网格使用.Datasource
并使用SQL查询y与DISTINCT
,例如:
string customers = SELECT DISTINCT * FROM Customers;
使用(SqlConnection con = new SqlConnection( 数据源= MY-PC;初始目录= northwind;集成安全性=真))
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(customers,con);
da.Fill(ds, Customers);
dataGridView.AutoGenerateColumns = true ;
dataGridView.DataSource = ds;
dataGridView.DataMember = Customers;
}
也可以在DataSet
过滤器 >(不一定是数据库查询)。
在VB.NET中:
< span class =code-keyword> Imports 系统
Imports System.Xml
Imports System.Data
Imports System.Data.SqlClient
公开 类程序
公共 共享 Sub Main()
Dim customers 作为 字符串 = SELECT DISTINCT * FROM Customers
使用 con 作为 SqlConnection = 新 SqlConnection( 数据源= MY-PC ;初始目录= northwind;集成安全性=真)
Dim ds As DataSet = 新 DataSet()
Dim da 作为 SqlDataAdapter = 新 SqlDataAdapter(customers,con)
da.Fill(ds, Customers)
dataGridView.AutoGenerateColumns = true
dataGridView。 DataSource = ds
dataGridView.DataMember = Customers
结束 使用
结束 Sub
结束 类跨度>
Hello everyone,
I have multiple datagridviews on my form in a tabcontrol, each one contain the following columns: "ref", "Bestand zu KHK (HW)", "Bestand Menge" and "Bestandsart".
the problem is that "ref" contains repeated values, what I want to do is remove all the duplicated values and sum the corresponding "Bestand zu KHK (HW)", "Bestand Menge" into one.
What I have tried:
Dim i = t1 - 1 Do Until i = t2 Dim j = 0 Do Until j = datagridviews(i).RowCount If Not datagridviews(i).Rows(j).Cells("ref").Value.Equals(0) Then Dim s1 Dim s2 s1 = datagridviews(i).Rows(j).Cells("Bestand zu KHK (HW)").Value s2 = datagridviews(i).Rows(j).Cells("Bestand Menge").Value For t = j + 1 To datagridviews(i).RowCount - 1 If datagridviews(i).Rows(j).Cells("ref").Value.Equals(datagridviews(i).Rows(t).Cells("ref").Value) And datagridviews(i).Rows(j).Cells("Bestandsart").Value.Equals(datagridviews(i).Rows(t).Cells("Bestandsart").Value) Then s1 = s1 + datagridviews(i).Rows(t).Cells("Bestand zu KHK (HW)").Value s2 = s2 + datagridviews(i).Rows(t).Cells("Bestand Menge").Value datagridviews(i).Rows(t).Cells("ref").Value = 0 datagridviews(i).Rows(t).Cells("Bestandsart").Value = 0 datagridviews(i).Rows(t).Cells("Bestand Menge").Value = 0 datagridviews(i).Rows(t).Cells("Bestand zu KHK (HW)").Value = 0 End If Next datagridviews(i).Rows(j).Cells("Bestand zu KHK (HW)").Value = s1 datagridviews(i).Rows(j).Cells("Bestand Menge").Value = s2 j = j + 1 ElseIf datagridviews(i).Rows(j).Cells("ref").Value.Equals(0) Then j = j + 1 End If Loop BackgroundWorker1.ReportProgress((i - (t1 - 1) + 1) / (t2 - t1 + 1) * 100) i = i + 1 Loop
once I click a button to execute the code, I don't get an error but it returns partial sums meaning that I still see some repeated values and I have to press one more time to do the job.
currently there is 21 datagridviews and each contains around 1100 row.
can someone please help or point in the right direction.
thanks in advance.
You could use a.Datasource
for your grid and use an SQL query withDISTINCT
, example:
string customers = "SELECT DISTINCT * FROM Customers"; using (SqlConnection con = new SqlConnection("Data Source=MY-PC;Initial Catalog=northwind;Integrated Security=True")) { DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(customers, con); da.Fill(ds, "Customers"); dataGridView.AutoGenerateColumns = true; dataGridView.DataSource = ds; dataGridView.DataMember = "Customers"; }
It is also possible to set a filter on aDataSet
(does not have to be a database query).
In VB.NET:
Imports System Imports System.Xml Imports System.Data Imports System.Data.SqlClient Public Class Program Public Shared Sub Main() Dim customers As String = "SELECT DISTINCT * FROM Customers" Using con As SqlConnection = New SqlConnection("Data Source=MY-PC;Initial Catalog=northwind;Integrated Security=True") Dim ds As DataSet = New DataSet() Dim da As SqlDataAdapter = New SqlDataAdapter(customers, con) da.Fill(ds, "Customers") dataGridView.AutoGenerateColumns = true dataGridView.DataSource = ds dataGridView.DataMember = "Customers" End Using End Sub End Class
这篇关于在VB.NET中对多个datagridviews中的重复值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!