Comma Seperated to Datagridview?

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.
This topic is 7 years and 2 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Comma Seperated to Datagridview?

Post by supportMIB »

Hello, I have an array of comma seperated values that look like:

Code: Select all

$names
Files, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True
Finance, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False
Loans, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False,False, True
How can I add each of these lines to the datagridview? It has all the columns prebuild for each item in the array.

This adds just to the first column...do i need to do a split and add? If so im not sure how to do that with a datagridview

Code: Select all

foreach($name in $names)
{
    $datagridview1.Rows.add($name)
}
Edit:

I tried this code, but still no luck...

Code: Select all

$i = 0
		foreach ($name in $names)
		{
			
			$spilt = $names.split(",")
	
			for ($x = 0; $x -ne $split.count; $x++)
			{
				$datagridview1.rows[$i].Cells[$x].Value = $split[$x]
			}

			$i++
		}
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Comma Seperated to Datagridview?

Post by supportMIB »

Nevermind...my 2nd example does work...but only without the typos! ($split vs $spilt)

Any other examples would be greatly appreciated though!

Edit..again...: so this only works if there is only 1 row...multiple rows does not work

Code: Select all

$i = 0
		foreach ($name in $names)
		{
			
			$spilt = $name.split(",")

			for ($x = 0; $x -ne $spilt.count; ($x++ - 1))
			{
				$datagridview1.rows[$i].Cells[$x].Value = $spilt[$x]
			}

			$i++
		}
with error:

Code: Select all

ERROR: Cannot index into a null array.
CC_User[Codebox=powershell file=Untitled.ps1][/Codebox].psf (462, 5): ERROR: At Line: 462 char: 5
ERROR: +                     $datagridview1.rows[$i].Cells[$x].Value = $spilt[$x]
ERROR: +                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : NullArray
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Comma Seperated to Datagridview?

Post by jvierra »

Use Import-Csv and add a header. Use Load-DataGrid to load the CSV into the grid.
This topic is 7 years and 2 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked