2011-12-30 95 views
1

我有一些問題來顯示值,但每次它重複datagridview中的值,我使用Microsoft Visual C#2005和框架2.0。Datagridview,只顯示唯一值重複單元格值C#2005

當我在編程時,我發現在循環內我需要檢查重複的值並計算它們,如果出現新值顯示值併發送郵件,我確實通過smtp爲郵件編寫代碼,但我需要重複的值進行計數和消除,只留下原始單元格和其餘的更新,這是可能的,這是連接到網格的代碼,我需要嚴格的幫助這個數據生成的代碼,因爲我避風港沒有在網上找到正確的代碼來有效地完成這項工作。

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



         ///************************************************* 
         /// Interacting with the pcs in the domain 
         ///************************************************* 

         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 

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

          ///************************************************* 
          /// discovered time 
          ///************************************************* 
          discovered = DateTime.Now; 


          ///************************************************* 
          /// Add the data to the datagridview 
          ///************************************************* 

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

        ///Using Unique doesent work, this was one of the solutions found 
          //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"); 




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


     } 

回答

3

而不是每次嘗試使用DataTable.Select或DataTable.Rows.Find檢查重複時添加新行。如果沒有重複添加一個新的新行,如果它已經存在,只需更新其他列。

另外,您在循環的每次迭代中都設置了DataSource,您只需要這樣做一次。

下面是一個簡單的不完整示例,它每秒更新一次網格,您應該能夠根據您的程序調整邏輯。

public partial class Form1 : Form 
    { 
     private readonly DataGridView _gridView; 
     private readonly DataTable _dataTable; 

     public Form1() 
     { 
      InitializeComponent(); 

      _dataTable = new DataTable(); 
      DataColumn computerColumn = new DataColumn("Name"); 
      _dataTable.Columns.Add(computerColumn); 
      _dataTable.Columns.Add(new DataColumn("IP")); 
      _dataTable.Columns.Add(new DataColumn("MAC")); 
      _dataTable.Columns.Add(new DataColumn("Descubierto")); 
      _dataTable.PrimaryKey = new [] { computerColumn }; 

      _gridView = new DataGridView 
          { 
           Dock = DockStyle.Fill, 
           DataSource = _dataTable 
          }; 
      Controls.Add(_gridView); 

      System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); 
      timer.Interval = 1000; 
      timer.Tick += TimerTick;  
      timer.Start(); 
     } 

     void TimerTick(object sender, EventArgs e) 
     { 
      DirectoryEntry domainEntry = new DirectoryEntry("WinNT://mydomain"); 
      domainEntry.Children.SchemaFilter.Add("Computer"); 

      _dataTable.BeginLoadData(); 

      foreach (DirectoryEntry machine in domainEntry.Children) 
      { 
       DataRow row = _dataTable.Rows.Find(machine.Name); 

       if(row == null) 
       { 
        row = _dataTable.NewRow(); 
        row[0] = machine.Name; 
        _dataTable.Rows.Add(row); 
       } 

       row[3] = DateTime.Now.ToString(); 
      } 

      _dataTable.EndLoadData(); 
     } 
    } 
+0

完美的,善意的提醒,我不是一個程序員,這是我的論文的一部分....即時通訊建立一個遠程廣告managemet機器人的域管理員 – 2011-12-31 20:42:29

+0

,那麼你可能想購買一本好書或找在互聯網上的好教程。加里有一個很好的答案,你又發佈了[這裏](http://stackoverflow.com/questions/8717898/datagridview-find-duplicate-rows-and-update-existing-data/8721553#8721553) – 2012-01-06 03:21:06

相關問題