-
March 3rd, 2020, 12:58 AM
#1
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
-
March 4th, 2020, 03:52 PM
#2
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);
-
March 6th, 2020, 04:01 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|