Datagridview,仅显示唯一值为Duplicate Cell Values C#2005

我有一些问题要显示值,但每次重复datagridview中的值,我使用Microsoft Visual C#2005和框架2.0。

当我编程时,我发现在循环内部我需要检查重复值并计算它们,如果出现新值显示值并发送邮件,我确实通过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); } } 

每次尝试使用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(); } }