Datgridview confusion

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 6 years and 6 months 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
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Datgridview confusion

Post by swindmiller »

Long story as short as I can. I have a CSV with Name,Card. I want to have a textbox that you input a card # and populate the result in the datagridview from the CSV contents, and continue to append to it. Basically people will input their card number to "check in" to a meeting, this will append to the datagridview until the end then I can export to another CSV. I have all this working, probably the wrong way, but cannot export the results to to a CSV. Now I know I have to "convert" the inputted CSV to a Datatable and thats where I am confused.
This is basically what I have so far:

Code: Select all

$form1_Load = {
$dataGridView1.ColumnCount = 2
	$dataGridView1.ColumnHeadersVisible = $true
	$dataGridView1.Columns[0].Name = "Name"
	$dataGridView1.Columns[1].Name = "Card1"
	}
	
	$buttonSubmit_Click = {
	$Global:cardnumber = $textboxScandata.Text
	$importstatement = Import-Csv $UsersDB | where { $_.Card1 -eq $cardnumber }
	If ($importstatement)
	{
			$importstatement | foreach {
			$datagridview1.Rows.add($_.Name, $_.Card1)
			}
			}
}
This works just fine but I cannot export to a csv. I know running the Import-CSV each time the submit button is press is not the right way to do it but I don't know how to write it to filter by the card number specified then append that to the datagridview. Each way I try it re-creates the datagridview with only the last record I searched for.

Can someone bump me in the right direction or give an example to follow.

Thanks,
Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datgridview confusion

Post by jvierra »

You would start by creating a DataTable and binding I to the grid. After that you add rows to the DataTable and they will appear in the grid. Last just export the DataTable.
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Datgridview confusion

Post by swindmiller »

Thanks!
This topic is 6 years and 6 months 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