CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2018
    Posts
    6

    Datagrid-forloop behavior

    Hello experts
    I have this behavior that I cannot figure it out. The following code is triggered by a button

    Code:
    try
    {
    
    Type typeFWPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
    INetFwPolicy2 fwPolicy2 = 				  (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);
    
    foreach (INetFwRule rule in fwPolicy2.Rules)
    	{
    	lvItems.RuleName = rule.Name; ;
    	lvItems.RemoteAddress = rule.RemoteAddresses;
    	lvItems.Protocol = rule.Protocol.ToString();
    	lvItems.LocalPort = rule.LocalPorts;
    	dataGrid1.Items.Add(lvItems);
    	//MessageBox.Show("close me");
    }
    
    
    }
    catch (Exception ex)
    {
    
    MessageBox.Show("something went wrong");
    
    }
    When I run it, the datagrid is populated with the same record (first) entry that is repeated several times.
    then I added a Messagebox to troubleshoot, then Datagrid is populated correctly but I need to acknowledge each Messagebox !!!!. It is like it just needs that extra mouse click.
    any help is greatly appreciated
    Last edited by 2kaud; March 3rd, 2020 at 04:23 AM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Datagrid-forloop behavior

    Use Debug.Writeline instead of the blocking MessageBox.Show. Start your app with F5 in VS and look in the debug window to see the output.

    Use something like this inside the loop:
    Code:
    Debug.WriteLine(rule.Name);

  3. #3
    Join Date
    Dec 2018
    Posts
    6

    Re: Datagrid-forloop behavior

    Many thanks Arjay,
    The reason was due to the object lvItems. I created the object outside the foreach loop, once I moved it inside, it worked fine.
    thanks for your reply.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured