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
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);
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.