Datagridview,查找重复的行并更新现有数据

本文关键字:更新 数据 查找 Datagridview | 更新日期: 2023-09-27 18:19:32

可能重复:
Datagridview,只显示唯一值是重复单元格值C#2005

我想知道如何在数据网格视图中找到重复的行,如果存在,只更新数据。。我需要在C#和.NET framework 2.0 VS2005中工作的代码。我没有使用来自Active Directory查询的数据库,并且值继续重复。

有人能帮我做这个吗。。。感谢

Datagridview,查找重复的行并更新现有数据

您需要遍历每一行,然后遍历每一列。最好的方法是在将它们放入DataGridView之前找到重复项,因为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
        }
    }
}

注意1:您的数据可能有一种模式,可以减少比较次数。此示例将每一行与后面的每一行进行比较

注意2:如果您想删除重复项,您应该从最后一个到第一个迭代行

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);
            }

        }