本文介绍了如何从datagridview1获取不同的行并传递到datagridview2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个代码,它从datagridview1中选择不同的值并将结果传递给datagridview2:



更多详情:

来自datagridview1



I got this code where it select distinct values from datagridview1 and pass the result to datagridview2:

Further Details:
Data from datagridview1

Quote:

代码金额

Pro1 100.00

Pro2 300.00

Pro1 100.00

Pro1 100.00

Pro2 300.00

Code Amount
Pro1 100.00
Pro2 300.00
Pro1 100.00
Pro1 100.00
Pro2 300.00







Quote:

结果应该放在datagridview2中,如下所示;



代码金额

Pro1 100.00 3

Pro2 300.00 2

The result should be placed in datagridview2 like below;

Code Amount Count
Pro1 100.00 3
Pro2 300.00 2





我的尝试:





What I have tried:

Dim dic As New Dictionary(Of String, Integer)()
            Dim cellValue As String = Nothing
            For i As Integer = 0 To dgvSubjectsEnrolled.Rows.Count - 1
                If Not dgvSubjectsEnrolled.Rows(i).IsNewRow Then
                    If dgvSubjectsEnrolled(0, i).Value > 0 Then
                        cellValue = dgvSubjectsEnrolled(0, i).Value.ToString()
                        If Not dic.ContainsKey(cellValue) Then
                            dic.Add(cellValue, 1)
                        Else
                            dic(cellValue) += 1
                        End If
                    Else
                    End If
    
                End If
            Next
    
            Dim sb1 As New StringBuilder()
            Dim sb2 As New StringBuilder()
            For Each keyvalue As KeyValuePair(Of String, Integer) In dic
                sb1.AppendLine(String.Format("{0}", keyvalue.Key, keyvalue.Value))
                sb2.AppendLine(String.Format("{1}", keyvalue.Key, keyvalue.Value))
            Next
            Me.dgvsub.Rows.Add(sb1.ToString, sb2.ToString())







Quote:

代码成功执行但结果是:



代码数

Pro1Pro2 32



数据与同一行连接。下一个值应该在下一行。

请编辑此代码。

The code executed successfully but the result is:

Code Count
Pro1Pro2 32

The data was concatenated with the same row. The next value should be at the next row.
Please edit this code.

推荐答案

private void Form1_Load(object sender, EventArgs e)
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("Code");
          dt.Columns.Add("Amount");
          dt.Rows.Add("Pro1", 100.00);
          dt.Rows.Add("Pro2", 300.00);
          dt.Rows.Add("Pro1", 100.00);
          dt.Rows.Add("Pro1", 100.00);
          dt.Rows.Add("Pro2", 300.00);
          dataGridView1.DataSource = dt;

          DataTable dt2 = new DataTable();
          dt2.Columns.Add("Code");
          dt2.Columns.Add("Amount");
          dt2.Columns.Add("Count");


          List<string> lstDistinct = new List<string>();  // stores the combination of Code_Amount
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
              string code = row.Cells[0].Value.ToString();
              string Amount = row.Cells[1].Value.ToString();
              lstDistinct.Add(code + "_" + Amount);
          }

          lstDistinct = lstDistinct.Distinct().ToList();  // Take distinct using linq

          foreach (string item in lstDistinct)  // iterate the distinct values
          {
              string code = item.Split('_')[0];
              string Amount = item.Split('_')[1];
              int count = 0;
              foreach (DataGridViewRow row in dataGridView1.Rows)  // search for the values in dgv1 to get the count
              {
                  string dgv1code = row.Cells[0].Value.ToString();
                  string dgv1Amount = row.Cells[1].Value.ToString();
                  if (code == dgv1code && Amount == dgv1Amount)  // comparision
                      count++;  // increament the counter if matches
              }
              dt2.Rows.Add(code, Amount, count);  // add the value to datatable
          }
          dataGridView2.DataSource = dt2;




      }


这篇关于如何从datagridview1获取不同的行并传递到datagridview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:08