Tuesday, May 25, 2010

How to propagate through a DataGridView control in C#



To propagate multiple Row selections in a DataGridView you need to use a foreach command for the Rows. Here is a piece of code from my app RPK, Remote Process Killer:


         foreach (DataGridViewRow row in dataGridViewResults.SelectedRows)
                    {
                        //  MessageBox.Show(row.Cells[2].Value.ToString());
                        string killProc = row.Cells[29].Value.ToString();



                        System.Management.ConnectionOptions
                        conO = new System.Management.ConnectionOptions();

                        System.Management.ManagementScope
                        oMs = new System.Management.ManagementScope
                            (@"\\" + txtMachineName.Text +
                            @"\root\cimv2", conO);

                        //get all the Processes running on remote machine

                        System.Management.ObjectQuery oQuery =
                            new System.Management.ObjectQuery
                                ("Select * from Win32_Process");

                        //Execute the query

                        System.Management.ManagementObjectSearcher
                            oSearcher =
                            new System.Management.ManagementObjectSearcher(
                                oMs, oQuery);

                        //Get the results

                        System.Management.ManagementObjectCollection
                            oReturnCollection = oSearcher.Get();



                        foreach (System.Management.ManagementObject
                            oReturn in oReturnCollection)
                        {
                            string[] argList = new string[] { string.Empty };

                            //Name of process
                            if (oReturn["ProcessId"].ToString().ToLower() ==
                                killProc)
                            {
                                object[] obj = new object[] { 0 };
                                oReturn.InvokeMethod("Terminate", obj);
                            }
                        }



                        ntyRPK.ShowBalloonTip
                                  (5000,
                                  appTitle,
                                  "Process Killed! " +
                                  row.Cells[2].Value.ToString() +
                                  " on " + txtMachineName.Text,
                                  ToolTipIcon.Warning);

                        this.refreshProcess();

                    }

No comments:

Post a Comment