DataGridView paint rows headers number starting to 1

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 9 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
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

DataGridView paint rows headers number starting to 1

Post by ramses147 »

Hi guys,

this code write rows headers number but the first row is '0', is it possible to start to 1 ?

Thanks
  1. $dataGridView.add_RowPostPaint($dataGridView_RowPostPaint)
  2. $datagridview_RowPostPaint = [System.Windows.Forms.DataGridViewRowPostPaintEventHandler]{
  3.        
  4.         if ($dataGridView.Rows[0].HeaderCell.Value) {
  5.             if ([int]($datagridview.Rows[$_.RowIndex].HeaderCell.Value) -ne [int]($_.RowIndex)) {
  6.  
  7.                 $dataGridView.Rows[$_.RowIndex].HeaderCell.Value = $_.RowIndex.ToString()
  8.             }
  9.         } else {
  10.             # this is to accommodate the first row issue
  11.             $dataGridView.Rows[0].HeaderCell.Value = '0'
  12.         }
  13.     }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView paint rows headers number starting to 1

Post by jvierra »

Just change the assignment to add 1 to each row and set the first row to be "1" instead of "0".
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: DataGridView paint rows headers number starting to 1

Post by ramses147 »

Is this right ?
  1. $dataGridView.Rows[$_.RowIndex].HeaderCell.Value = $_.RowIndex +1
  2.             }
  3.         } else {
  4.             $dataGridView.Rows[0].HeaderCell.Value = '1'
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: DataGridView paint rows headers number starting to 1

Post by ramses147 »

i tryed also with this, working but with update flashing
  1. $dataGridView.Rows[$_.RowIndex].HeaderCell.Value = ($_.RowIndex +1).ToString()
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: DataGridView paint rows headers number starting to 1

Post by ramses147 »

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

Re: DataGridView paint rows headers number starting to 1

Post by jvierra »

ramses147 wrote:any ideas ?
About what? If you did what I posted it will number from 1.
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: DataGridView paint rows headers number starting to 1

Post by ramses147 »

You wrote: Just change the assignment to add 1 to each row and set the first row to be "1" instead of "0".

i wrote this, but here in attachment the results
  1. $dataGridView.Rows[$_.RowIndex].HeaderCell.Value = $_.RowIndex +1
  2.             }
  3.         } else {
  4.             $dataGridView.Rows[0].HeaderCell.Value = '1'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView paint rows headers number starting to 1

Post by jvierra »

I cannot make it any easier.

Code: Select all

$datagridview1_RowPostPaint=[System.Windows.Forms.DataGridViewRowPostPaintEventHandler]{

	if($datagridview1.Rows[0].HeaderCell.Value){
		if([int]($datagridview1.Rows[$_.RowIndex].HeaderCell.Value - 1) -ne [int]($_.RowIndex)){
			$datagridview1.Rows[$_.RowIndex].HeaderCell.Value = ($_.RowIndex + 1).ToString()
		}
	}else{
		# this is to accommodate the first row issue
		$datagridview1.Rows[0].HeaderCell.Value = '1'
	}
	
}
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: DataGridView paint rows headers number starting to 1

Post by ramses147 »

Cool, now working right, many thanks ! :!:
This topic is 6 years and 9 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