Datagridview,只显示独特的值是重复单元格值C#2005 [英] Datagridview, Only show unique values were Duplicate Cell Values C# 2005

查看:325
本文介绍了Datagridview,只显示独特的值是重复单元格值C#2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some issues to display values but every time it duplicates the values in the datagridview, I’m using Microsoft Visual C# 2005 and framework 2.0.

当我在编程这个我发现在循环内我需要检查重复的值和计数,如果一个新的值出现显示值和发送邮件,我确实有通过smtp邮件的代码,但我需要重复的值要计数和消除只剩下原始单元格并更新其余的,可以这样做,这是连接到网格和数据生成的代码,我需要严重的帮助,因为我没有找到正确的代码在网络上有效地完成这项工作。

As I was programming this I found that inside the loop I need to check for duplicated values and count them and if a new value appears display the value and send a mail, I do have the code for the mail via smtp, but I need the duplicate values to be counted and eliminated only leaving the original cell and updating the rest, Can that be possible, this is the code that’s connected to the grid and the data generation I need serious help with this one, because I haven’t found the correct code on the web to do this job efficiently.

  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添加新行来检查一个副本。如果没有重复添加新的一行,如果它已经存在只是更新其他列。

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.

此外,您在循环的每次迭代中设置DataSource,您只需执行一次。

Also you're setting the DataSource on every iteration of the loop, you only need to do this once.

这是一个简单的不完整的例子,每秒更新网格,你应该能够适应你的程序的逻辑。

Here is a simple incomplete example that updates the grid every second, you should be able to adapt the logic here for your program.

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

这篇关于Datagridview,只显示独特的值是重复单元格值C#2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆