Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of adding a new row every time try using DataTable.Select or DataTable.Rows.Find to check for a duplicate. If there is no duplicate add a new new row, if it already exists just update the other columns. </p> <p>Also you're setting the DataSource on every iteration of the loop, you only need to do this once.</p> <p>Here is a simple incomplete example that updates the grid every second, you should be able to adapt the logic here for your program.</p> <pre><code> 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload