简体   繁体   English

如何删除DataGridView中的重复数据

[英]How To Remove Duplication Of Data In DataGridView

I am developing a VB.NET application that uses a DataGridView control to display data from a database. 我正在开发一个VB.NET应用程序,该应用程序使用DataGridView控件显示数据库中的数据。 Currently, it is displaying as follows: 当前,它显示如下:

54     john         NJ        HRA           1000   
54     john         NJ        DA            2500   
54     john         NJ        BP            12500

But I need to display it as follows: 但是我需要显示如下:

54      john  NJ
                     HRA           1000   
                     DA            2500
                     BP            12500

Since ID, name and city are repeated, I do not need to display these again. 由于重复了ID,名称和城市,因此无需再次显示。 How can I do this? 我怎样才能做到这一点?

I think this can help you 我认为这可以帮助您

DataTable dtDataSource = new DataTable();

dtDataSource.Columns.Add("A");
dtDataSource.Columns.Add("B");
dtDataSource.Columns.Add("C");

dtDataSource.Rows.Add(new object[] { "1", "1", "1" });
dtDataSource.Rows.Add(new object[] { "1", "1", "2" });
dtDataSource.Rows.Add(new object[] { "1", "1", "3" });
dtDataSource.Rows.Add(new object[] { "2", "1", "1" });
dtDataSource.Rows.Add(new object[] { "2", "1", "2" });
dtDataSource.Rows.Add(new object[] { "2", "1", "3" });

DataTable dtNew = dtDataSource.Clone();

for (int i = 0; i < dtDataSource.Rows.Count; i++)
{
    if (i > 0)
    {
        if (dtDataSource.Rows[i][0].Equals(dtDataSource.Rows[i - 1][0]))
        {
            dtNew.Rows.Add(new object[] { "", dtDataSource.Rows[i][1], dtDataSource.Rows[i][2] });
        }
        else
        {
            dtNew.Rows.Add(new object[] { dtDataSource.Rows[i][0], "", "" });
            dtNew.Rows.Add(new object[] { "", dtDataSource.Rows[i][1], dtDataSource.Rows[i][2] });
        }
    }
    else
    {
        dtNew.Rows.Add(new object[] { dtDataSource.Rows[i][0], "", "" });
        dtNew.Rows.Add(new object[] { "", dtDataSource.Rows[i][1], dtDataSource.Rows[i][2] });
    }
}

dataGridView1.DataSource = dtNew;

Your data should be ordered initially. 您的数据应首先订购。

I tried to convert VB.Net here 我试图在这里转换VB.Net

If all you are doing is displaying data you could use a UNION of two SQL statements. 如果您要做的只是显示数据,则可以使用两个SQL语句的UNION。

The first would retrieve the first three columns plus two columns with empty strings UNION SQL three columns with empty strings and the two columns of data. 第一个将检索前三列以及带空字符串的两列UNION SQL带空字符串的三列和数据的两列。 The field names of the first SQL statement will name the columns. 第一个SQL语句的字段名称将为列命名。

The data would be read only. 数据将是只读的。

    SELECT F1, F2, F3, '' as F4, '' as F5 WHERE ...
    UNION
    SELECT '', '', '', F4, F5 WHERE ...

If you are retrieving data for more than one target the SQL will get a bit more complex. 如果您要检索多个目标的数据,则SQL会变得更加复杂。

your problem is occurring because of how your database is made. 您的问题是由于数据库的制作方式引起的。

are you using primary keys and foreign keys? 您在使用主键和外键吗?

so that you can easily identify which records only to display 这样您就可以轻松确定仅显示哪些记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM