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.
jvierra
Posts: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 20, 2019 4:27 pm

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
Joined: Tue Nov 05, 2019 3:44 pm

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

Post by pjbuckley » Wed Nov 20, 2019 4:57 pm

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 20, 2019 5:40 pm

You can't use data binding without a DataTable or other bindable object.

pjbuckley
Posts: 31
Joined: Tue Nov 05, 2019 3:44 pm

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

Post by pjbuckley » Wed Nov 20, 2019 7:51 pm

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 20, 2019 8:49 pm

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 20, 2019 9:11 pm

Here is a full, hand coded, version that runs and shows how this works.
Attachments
HandCodedDGV.ps1
(1.04 KiB) Downloaded 9 times

pjbuckley
Posts: 31
Joined: Tue Nov 05, 2019 3:44 pm

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

Post by pjbuckley » Thu Nov 21, 2019 8:30 am

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
Joined: Tue Nov 05, 2019 3:44 pm

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

Post by pjbuckley » Thu Nov 21, 2019 12:07 pm

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Nov 21, 2019 1:06 pm

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Nov 21, 2019 2:38 pm

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 213 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
}

Post Reply