I am writing this in C# WPF using Powershell Commands.
I first create the Datagrid column objects:
public class Item
{
public object ID { get; set; }
public object ProcessName { get; set; }
public object Handles { get; set; }
public object NPM { get; set; }
public object PM { get; set; }
public object WS { get; set; }
public object Path { get; set; }
}
Then the following code populates a Datagrid that shows all running processes on my PC:
private void btnGetProcess_Click(object sender, RoutedEventArgs e)
{
//Creates fields
DataGridTextColumn c1 = new DataGridTextColumn();
c1.Header = "ID";
c1.Binding = new Binding("ID");
dgdProcessList.Columns.Add(c1);
DataGridTextColumn c2 = new DataGridTextColumn();
c2.Header = "Process Name";
c2.Binding = new Binding("ProcessName");
dgdProcessList.Columns.Add(c2);
DataGridTextColumn c3 = new DataGridTextColumn();
c3.Header = "Handles";
c3.Binding = new Binding("Handles");
dgdProcessList.Columns.Add(c3);
DataGridTextColumn c4 = new DataGridTextColumn();
c4.Header = "NPM(K)";
c4.Binding = new Binding("NPM");
dgdProcessList.Columns.Add(c4);
DataGridTextColumn c5 = new DataGridTextColumn();
c5.Header = "PM(K)";
c5.Binding = new Binding("PM");
dgdProcessList.Columns.Add(c5);
DataGridTextColumn c6 = new DataGridTextColumn();
c6.Header = "WS(K)";
c6.Binding = new Binding("WS");
dgdProcessList.Columns.Add(c6);
DataGridTextColumn c7 = new DataGridTextColumn();
c6.Header = "Path";
c6.Binding = new Binding("Path");
dgdProcessList.Columns.Add(c7);
//Connection to hostname successful
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell ps = PowerShell.Create(); // Create a new PowerShell instance
ps.Runspace = runspace; // Add the instance to the runspace
ps.Commands.AddScript("Get-Process | Select-Object ID, ProcessName, Handles, NPM, PM, WS, Path"); // Add a script
Collection<PSObject> results = ps.Invoke();
runspace.Close();
foreach (PSObject obj in results)
{
object IDOutput;
object ProcessNameOutput;
object HandlesOutput;
object NPMOutput;
object PMOutput;
object WSOutput;
object PathOutput;
IDOutput = obj.Properties["ID"].Value;
ProcessNameOutput = obj.Properties["ProcessName"].Value;
HandlesOutput = obj.Properties["Handles"].Value;
NPMOutput = obj.Properties["NPM"].Value;
PMOutput = obj.Properties["PM"].Value;
WSOutput = obj.Properties["WS"].Value;
PathOutput = obj.Properties["Path"].Value;
dgdProcessList.Items.Add(new Item() { ID = IDOutput, ProcessName = ProcessNameOutput, Handles = HandlesOutput, NPM = NPMOutput, PM = PMOutput, WS = WSOutput, Path = PathOutput });
}
}
I'm trying to figure out how to get the selected row value. More particularly, I want to be able to get the "ID" field value of the row I have selected.
I've spent the last few hours playing with different property variations (SelectedItem, SelectedValue, GetValue, etc.), but I can't seem to get it working. For example, this code returns a value (ProjectName.FormName+Item), but not the actual object value:
MessageBox.Show(dgdProcessList.SelectedValue.ToString());
Any help is appreciated! Thank you.
Item
? – Mathias R. JessendgdProcessList.Items.Add(new Item(){...})
<-- what isItem
? – Mathias R. Jessen