Populate datagridview with checkboxes from SQL database

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 1 year and 11 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
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Populate datagridview with checkboxes from SQL database

Post by supportMIB »

Product, version and build: Powershell Studio 2022
Operating system: Windows 10
PowerShell version(s): 5+

I have a database that is structured something like...

Name,custID,Cover,overview,glossary
bank1,102,1,0,1
bank2,103,0,1,1
etc

I'd like to populate a datagridview with checkboxes that is populated from the above database. The checkboxes would be checked if the value of the row =1 and unchecked if = 0....is there a way to accomplish this?
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 1:00 pm
Answers: 39
Been upvoted: 30 times

Re: Populate datagridview with checkboxes from SQL database

Post by brittneyr »

[Topic moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populate datagridview with checkboxes from SQL database

Post by jvierra »

You have to set the Checkbox properties to use the data values as checked or unchecked. Edit the column cell properties and set the required values.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populate datagridview with checkboxes from SQL database

Post by jvierra »

Screenshot 2022-03-30 202740.png
Screenshot 2022-03-30 202740.png (26.14 KiB) Viewed 1012 times
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Populate datagridview with checkboxes from SQL database

Post by supportMIB »

I guess I'm still a little unsure how to import the SQL table to the datagridview when the columns are predetermined, can you provide a small example how I would do that if I manually set up the columns for the datagridview?

I'm using the below code to create a datatable that i'm using to populate the grid...if i prebuild the columns how can i use a datatable to populate those columns?

Code: Select all

function Get-DatabaseTable
{
	[OutputType([System.Data.DataTable])]
	Param ()
	#Load the database assembly
	Add-Type -AssemblyName 'System.Data'
	#Database Query
	$QueryString = 'select * from NAME'
	$connection_string = "Server = 10.x.x.x; Database = Table; Integrated Security = True; MultipleActiveResultSets=true;"
	$connection = New-Object System.Data.SqlClient.SqlConnection
	$connection.ConnectionString = $connection_string
	$connection.Open()
	$command = New-Object System.Data.SqlClient.SqlCommand
	$command.Connection = $connection
	$Command.CommandText = $querystring
	$adapter = New-Object System.Data.SqlClient.SqlDataAdapter ($command)
	$dataset = New-Object System.Data.DataSet
	[void]$adapter.Fill($dataset)
	#Return the Dataset
	return @( ,$dataset.Tables[0])
	
	$table = Get-DatabaseTable
	
	$datagridview1.DataSource = $table
	Update-DataGridView -DataGridView $datagridview2 -Item $table
}
Here's an example of what i'm trying to do
Image
This topic is 1 year and 11 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