PowerShell -Working With DataGridView- WinForm and DataTable

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
probles40
Posts: 15
Joined: Sat Apr 11, 2020 6:05 pm

PowerShell -Working With DataGridView- WinForm and DataTable

Post by probles40 »

Hi, Am having issue retrieving any data from the rss file. I need help with using DataTable, and continue to display inside of winform. Below, am adding the sample code that I have so far.

Code: Select all

[code]clear
$global:results = @()

#...searchindex function is here ....

Invoke-WebRequest -Uri 'https://*.*.*.rss' -OutFile C:\*.rss
[xml]$Content = Get-Content C:\*.rss
 $Feed = $Content.rss.channel
$url = foreach ($msg in $Feed.item){
 $msg.pubDate,$msg.title,
 $msg.description.InnerText -replace '<br ?/?>',[System.Environment]::NewLine -replace '<[^>]+>'
 }
$FormEvent_Load={
	$dataGridView = New-Object System.Data.DataTable
	$dataGridView.Columns.Add('Date', [int])
	$dataGridView.Columns.Add('Title', [string])
	$dataGridView.Columns.Add('Description', [string])
	$dataGridView.Columns[0].width = 70
	$dataGridView.Columns[1].width = 240
	$dataGridView.Columns[2].width = 70
	
	$form = New-Object System.Windows.Forms.Form
	$form.Size = New-Object System.Drawing.Size(900,600)
	$dataGridView = New-Object System.Windows.Forms.DataGridView
	$dataGridView.Size=New-Object System.Drawing.Size(800,400)
	$go = New-Object System.Windows.Forms.Button
	$go.Location = New-Object System.Drawing.Size(300,450)
	$go.Size = New-Object System.Drawing.Size(75,23)
	$go.text = "Select"
	$form.Controls.Add($go)
	$form.Controls.Add($dataGridView)

	$dataGridView = New-Object System.Collections.ArrayList
	$dataGridView.AddRange(($Url))
	$dataGridView.DataSource = [collections.arraylist]$url

}

$form.ShowDialog() 
[/code]

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by jvierra »

This is the answer I gave you in the other forum. Please understand what it is I am trying to show you:

Code: Select all

$url='https://s.ch9.ms/Feeds/RSS'
$content = [xml](invoke-webrequest $url).Content
$items = $content.rss.channel.item |
    ForEach-Object{
        [PSCustomObject]@{
            Date = [datetime]$_.pubDate
            Title = $_.title
            Summary = $_.Summary
            Category = [string]$_.category
        }
}

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Size = '900,600'

$dataGridView = New-Object System.Windows.Forms.DataGridView
$form.Controls.Add($dataGridView)
$dataGridView.Size = '800,400'
$dataGridView.SelectionMode = 'FullRowSelect'
$dataGridView.MultiSelect = $true
$dataGridView.DataSource = ConvertTo-DataTable $items
$dataGridView.Columns[0].width = 70
$dataGridView.Columns[1].width = 240
$dataGridView.Columns[2].width = 70

$go = New-Object System.Windows.Forms.Button
$form.Controls.Add($go)
$go.Location = '300,450'
$go.Size = '75,23'
$go.text = 'Select'

$form.ShowDialog() 

probles40
Posts: 15
Joined: Sat Apr 11, 2020 6:05 pm

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by probles40 »

The code as you are showing is giving an error.
The property 'width' cannot be found on this object. Verify that the property exists and can be set.
At C:\*.ps1:25 char:1
+ $dataGridView.Columns[2].width = 70
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by jvierra »

If the DGV is not populated that will cause an error. You must be sure that you have a DataTable. If there is no tabkle then there will be no columns to set.

probles40
Posts: 15
Joined: Sat Apr 11, 2020 6:05 pm

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by probles40 »

Thanks everyone! I added the function and now is pulling the correct information. The data that is been pulled, however, has some html tags. I was able to fix this issue by using this line.

@{Name="Descriptions";Expression = {$_.description.innertext -replace '<br ?/?>'

Seems to be working!

The text returned from the rss, it is pretty lenghty. There is a title and follow by description. Is there an way to show just the title, and if the user wants to read more to have an option to open the row or expand?

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by jvierra »

"Summary" is "Description" without the tags.

You clearly didn't use my code which means you are still doing this in a needlessly complicated way. Try to understand the code I posted. It will help you to learn PowerShell without guessing.

probles40
Posts: 15
Joined: Sat Apr 11, 2020 6:05 pm

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by probles40 »

ImageJVierra, I did use originally the code. But, in the xml file it does not exist summary only pubdate, title, and description. With your code, it is only pulling the first two, date and title.
Attachments
xml file
xml file
xml file.PNG (10.88 KiB) Viewed 582 times

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by jvierra »

All are part of "item". The image yo posted is not what I used. Even your image does not have "pubDate" or "date". You are only looking at the summary.

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by jvierra »

Code: Select all

PS C:\scripts>     $content = [xml](invoke-webrequest $url).Content
PS C:\scripts>     $items = $content.rss.channel.item
PS C:\scripts> $items |Gm -MemberType Properties


   TypeName: System.Xml.XmlElement

Name        MemberType Definition
----        ---------- ----------
author      Property   string author {get;set;}
category    Property   System.Object[] category {get;}
commentRss  Property   string commentRss {get;set;}
comments    Property   System.Object[] comments {get;}
creator     Property   string creator {get;set;}
description Property   System.Xml.XmlElement description {get;}
duration    Property   string duration {get;set;}
enclosure   Property   System.Xml.XmlElement enclosure {get;}
group       Property   System.Xml.XmlElement group {get;}
guid        Property   System.Xml.XmlElement guid {get;}
link        Property   string link {get;set;}
pubDate     Property   string pubDate {get;set;}
summary     Property   string summary {get;set;}
thumbnail   Property   System.Object[] thumbnail {get;}
title       Property   string title {get;set;}

probles40
Posts: 15
Joined: Sat Apr 11, 2020 6:05 pm

Re: PowerShell -Working With DataGridView- WinForm and DataTable

Post by probles40 »

Hi, this is what is returning...

TypeName: System.Xml.XmlElement

Name MemberType Definition
---- ---------- ----------
description Property System.Xml.XmlElement description {get;}
guid Property System.Xml.XmlElement guid {get;}
link Property System.Xml.XmlElement link {get;}
pubDate Property string pubDate {get;set;}
title Property string title {get;set;}

Locked