Datagridview, find duplicate rows and update exist

2019-06-13 17:19发布

Possible Duplicate:
Datagridview, Only show unique values were Duplicate Cell Values C# 2005

I want to know how to find duplicate rows in a datagridview and if exist only update the data.. i need the code to do that I'm working in C# and .NET framework 2.0 VS2005. I'm not using a database is from query from Active Directory and the values continue repeating.

can someone helpme with this...Thanks

2条回答
看我几分像从前
2楼-- · 2019-06-13 17:57

try {

                e.Result = "";

                //int count1 = 0;
                int val = 6000;

                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ComputerName", typeof(String)));          //0
                dt.Columns.Add(new DataColumn("IP", typeof(String)));            //1
                dt.Columns.Add(new DataColumn("MAC", typeof(String)));       //2
                dt.Columns.Add(new DataColumn("Descubierto", typeof(String)));  

                for (int a = 1; a <= val; a++)


                {


                    counter.Text = Convert.ToString(a);
                    if (txtWorkGroupName.Text == "") return;

                    //DataTable dt = new DataTable();
                    //dt.Clear();


                        //3
                    //int i = 0;


                    try
                    {
                        // Datos del grupo WinNT://&&&&(Nombre del grupo de trabajo)
                        DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + txtWorkGroupName.Text + "");
                        DomainEntry.Children.SchemaFilter.Add("Computer");



                        ///*************************************************
                        /// Interactuando con PC's dentro del mismo dominio
                        ///*************************************************

                        foreach (DirectoryEntry machine in DomainEntry.Children)
                        {

                            string strMachineName = machine.Name;
                            string strMACAddress = "";
                            IPAddress IPAddress;
                            DateTime discovered;

                            try
                            {
                                IPAddress = getIPByName(machine.Name);

                            }
                            catch
                            {
                                continue;
                            }//try/catch

                            ///*************************************************
                            /// Obtener Mac
                            ///*************************************************
                            strMACAddress = getMACAddress(IPAddress);

                            discovered = DateTime.Now;


                            ///*************************************************
                            /// Manejo de repeticiones en el grid
                            ///*************************************************


                           bool isDuplicate;


                            for(int nbRow = 0; nbRow < dt.Rows.Count; nbRow++)
                            {
                                for(int nbRowCompare = nbRow; nbRowCompare < dt.Rows.Count; nbRowCompare++)
                                    {
                                        isDuplicate = true;

          for(int nbCol = 0; nbCol < dt.Rows[nbRow].Cells.Count; nbCol++)
                                {
                if(dt[nbCol, nbRow].Value != dt [nbCol, nbRowCompare])
                                             {
                             isDuplicate = false;
                          break;     //Exit for each column if they are not duplicate
                                              }
                                         }

                                    if(isDuplicate)
                                     {
                                         MessageBox.Show("el valor esta duplicado");
                                     }
                                }
                            }



                            ///*************************************************
                            /// this is gonna BE fixed
                            ///*************************************************





                            DataRow dr = dt.NewRow();


                            dr[0] = machine.Name;
                            dr[1] = IPAddress;
                            dr[2] = strMACAddress;
                            dr[3] = Convert.ToString(discovered);
                            dt.Rows.Add(dr);



                            dgvComputers1.DataSource = dt;



                            dgvComputers1.Refresh();
                            //dt.Columns(machine.Name).Unique = true;
                            //dt.Columns(IPAddress).Unique = true;
                            //dt.Columns(strMACAddress).Unique = true;




                        }//foreach loop





                       // DataView dv = new DataView();

                       // dv = dt;



                        Thread.Sleep(2000);

                        //dt = ((DataView)this.dgvComputers1.DataSource).Table;
                        //dt.WriteXml(@"testermac.xml");


                        ///*************************************************
                        /// SETTING DATASOURCE
                        ///*************************************************
                        //  dgvComputers.DataSource = dt;

                        //goto factor;


                        //factor:
                        //  if (restart < 20)
                        //{

                        //  btnScan.PerformClick();
                        // restart = restart + restart++;
                        //counter.Text = Convert.ToString(restart);

                        //}

                    }//try/catch

                    catch (Exception ex)
                    {
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }


                    if (backgroundWorker2.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                }

            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("error:" + ex);
                //tbmessage.Text += "se ha producido un error: " + ex + Environment.NewLine;
                //tbmessage.SelectionStart = tbmessage.Text.Length;
                //tbmessage.ScrollToCaret();
            }
            catch (NoNullAllowedException ex)
            {
                MessageBox.Show("error:" + ex);
            }
            catch (AccessViolationException ex)
            {
                MessageBox.Show("error:" + ex);
            }


        }
查看更多
We Are One
3楼-- · 2019-06-13 18:00

You'd need to loop through each row and then through each column. The best way would be to find duplicate before putting them in the DataGridView because there is no built-in way to do it with a DataGridView.

bool isDuplicate;

for(int nbRow = 0; nbRow < DataGridView1.Rows.Count; nbRow++)
{
    for(int nbRowCompare = nbRow; nbRowCompare < DataGridView1.Rows.Count; nbRowCompare++)
    {
        isDuplicate = true;

        for(int nbCol = 0; nbCol < DataGridView1.Rows[nbRow].Cells.Count; nbCol++)
        {
            if(DataGridView1[nbCol, nbRow].Value != DataGridView1­[nbCol, nbRowCompare])
            {
                isDuplicate = false;
                break;     //Exit for each column if they are not duplicate
            }
        }

        if(isDuplicate)
        {
             //Do something
        }
    }
}

Note 1: Your data might have a pattern where you can reduce the number of comparison. This example compare every row against every following rows.

Note 2: If you want to delete duplicates, you should iterate through the rows from the last to the first

查看更多
登录 后发表回答