Bind data to a datagridview datasource removes the row header

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 4 years and 4 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bind data to a datagridview datasource removes the row header

Post by jvierra »

That would be easy with a DataTable and columns for the schedule. It can be done in 10 or 15 lines of code. It requires no header adjustments and is a simple table with names and checkboxes. Checkboxes autogenerate over Boolean fields.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Bind data to a datagridview datasource removes the row header

Post by pjbuckley »

My databindings are in there. Probably just poorly executed. :shock: :D :lol:

You have opened my eyes and many doors for me in this project. It's been more fun than i thought it would be. Only slightly frustrating as a realize how much im missing. Im a broadsword where i need to be a scalpel.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bind data to a datagridview datasource removes the row header

Post by jvierra »

You can't use data binding without a DataTable or other bindable object.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Bind data to a datagridview datasource removes the row header

Post by pjbuckley »

Cant seem to get this bit of code to work:

Code: Select all

	$dgv1_RowPostPaint=[System.Windows.Forms.DataGridViewRowPostPaintEventHandler]{
		for($w=0;$w -le $Dgv1.rows.count;$w++){
		ForEach ($Thing in $SchedBind) {
				$DGV1.Rows[$w].HeaderCell.Value = "$($thing.StartTime + '-' + $Thing.FinishTime + '  -->  ' + $Thing.Activity)"
			}
		}
	}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bind data to a datagridview datasource removes the row header

Post by jvierra »

Start simple. Make this work as-is:

Code: Select all

$dgv1_RowPostPaint=[System.Windows.Forms.DataGridViewRowPostPaintEventHandler]{
    for($w=0;$w -lt $dgv1.rows.count;$w++){
        $DGV1.Rows[$w].HeaderCell.Value = "$w"
    }
}
Once you fix your code so that this works correctly then change what is being set on the row value.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bind data to a datagridview datasource removes the row header

Post by jvierra »

Here is a full, hand coded, version that runs and shows how this works.
Attachments
HandCodedDGV.ps1
(1.04 KiB) Downloaded 78 times
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Bind data to a datagridview datasource removes the row header

Post by pjbuckley »

Here is the problem. None of my datasources contain the data that i am trying to capture. I am creating the data after ive populated the datagrid with checkboxes. I am using the datasources to populate the Row and column headers, the results being the checked boxes.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Bind data to a datagridview datasource removes the row header

Post by pjbuckley »

This still doesnt help me dynamically build the row headers from a column in a table.

Once again there is no datasource for the checkboxes in any of the tables that are created before building the datagrid. I am trying to dynamically generate row and column headers around checkbox columns to take attendance. Creating a completely new dataset.

Every way i look at this i need to get to the unbound checkbox data and manipulate it. Unless i am approaching this completely wrong.

Each datatable that i generate has completely different schemas and dont match up to rows or columns between each other. I need the grid to be a seperate dataset (i guess) built like ive described to take attendance.

Attendance data doesnt exist until i check a box in the grid at which point once attendance is taken the data needs to be exported to an all new CSV
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bind data to a datagridview datasource removes the row header

Post by jvierra »

If you have no data then there is no way to build a grid. Why would you want a grid with no data.

If you are trying to build a form with checkboxes in columns and labels on the left then you will want to use the Table Control as it can layout the other controls as a table and can be built in a similar way to what you are attempting.

Overall we need to learn how forms are designed to work and how we use the default controls.

It is possible and simple to create a grid that has a DataTable that is used to generate checkboxes. In this case we would use the left column for labels and create Boolean data fields named by the column headers. This would then let you collect checks as needed and read them out from the DataTable and convert to anything you want. First just build a DataTable that has a field for the row label and a field for each checkbox then just assign it to a blank grid. It is that simple.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bind data to a datagridview datasource removes the row header

Post by jvierra »

I decided t do a demo of a dynamically generated checked grid based on a DGV. Using PSS it took about 10 minutes but I have attached a PS1 file so you can easily run it.
Annotation 2019-11-21 163452.png
Annotation 2019-11-21 163452.png (10.2 KiB) Viewed 1927 times
Here is 5the only code I had to write and it does everything you see in the screenshot.

Code: Select all

$labels = 'Label 1','Label 2','Label 3','Label 4','Label 5','Label 6','Label 7','Label 8','Label 9'
$checks = 'Check1','Check2','Check3','Check4','Check5','Check6','Check7','Check8','Check9'

$dt = [System.Data.DataTable]::New()
$dt.Columns.Add('Label',[string])

foreach($check in $checks){
    $dt.Columns.Add($check,[bool])
}

$datagridviewResults.DataSource = $dt

$formMain_Load={
}

$buttonExit_Click={
	$formMain.Close()
}

$buttonLoad_Click= {
    $dt.Clear()  # clear for reload
    foreach($label in  $labels){
        $row = $dt.NewRow()
        $row['label'] = $label
        $dt.Rows.Add($row)
    }
    $datagridviewResults.Columns[0].ReadOnly = $true
}
This topic is 4 years and 4 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