0

Background:

I am creating a GUI using XML and PowerShell. I am using PowerShell to add functionality to the GUI. In my GUI, there are Comboboxes (drop-down meuns) as well as a textbox that filters the results of the Datagrid. I am currently working on altering the Datagrid with the results of a textbox.

What I'm Trying To Achieve:

The results in the Datagrid change with the text entered in a textbox. For example, if the user enters a "W", all the results that begin with a "W" will be listed.

The Problem:

I am having trouble altering the contents of the Datagrid. I have tried to edit the ItemsSource. This did not work because the Datagrid did not repopulate the table when the user retracts characters from the textbox. I have also tried to edit the Items property of the Datagrid. However, the items property is not editable. I found a solution to a similar problem here, but I couldn't utilize his solution in my current code.

Code:

add-type @"
    public class Server
    {
        public Server() {}

        public string Column1 { get; set; }
        public string Column2 { get; set; }
        public string Column3 { get; set; }
        public string Column4 { get; set; }
        public string Column5 { get; set; }
        public string Column6 { get; set; }
    }
"@ -Language CsharpVersion3


[System.Collections.ArrayList] $Server = New-Object "System.Collections.ArrayList"

$GUI | ForEach-Object {
$ServerToAdd = new-object Server
$ServerToAdd.Column1 = $_.Results1
$ServerToAdd.Column2 = $_.Results2
$ServerToAdd.Column3 = $_.Results3
$ServerToAdd.Column4 = $_.Results4
$ServerToAdd.Column5 = $_.Results5
$ServerToAdd.Column6 = $_.Results6

$Server.add($ServerToAdd) | Out-Null 
}

$GUI.DataGrid.ItemsSource = $Server

$GUI.TextBox.Add_TextChanged({

    $InputText = $GUI.TextBox.Text 

    $GUI.DataGrid.Items | Where-Object { $_.Column1.Contains($InputText) } 

}) 

1 Answer 1

1

I think it's best to make DataTable object instead of array.

You can then put it's as DataSource to you DataGrid control and then easily perform filtering like this:

$DataSource.DefaultView.RowFilter = "Column1 LIKE 'W%'"
$DataGrid.DataSource = $DataSource

I've made an example how that would work. I hope will get the idea.

$dt = New-Object system.Data.DataTable
$dt.Columns.Add((New-Object System.Data.DataColumn('Column1', 'string')))
$dt.Columns.Add((New-Object System.Data.DataColumn('Column2', 'int')))
$dt.Columns.Add((New-Object System.Data.DataColumn('Column3', 'string')))
$null=$dt.Rows.Add('RowOne',100,'This')
$null=$dt.Rows.Add('RowTwo',150,'That')
$null=$dt.Rows.Add('RowThree',200,'What')

$filter = "Column2 > 100 AND Column3 LIKE 'W%'"
$dt.DefaultView.RowFilter = $filter
$dt.DefaultView

Column1  Column2 Column3
-------  ------- -------
RowThree     200 What   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.