About the datagridview1.Rows.Add method of adding data to the specified column name row

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 5 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
chenzilong326
Posts: 3
Last visit: Fri Oct 28, 2022 8:24 am

About the datagridview1.Rows.Add method of adding data to the specified column name row

Post by chenzilong326 »

Suppose I have used powershell studio to add datagridview control to make 9 columns respectively column1..... .column9.

Usually you can add fewer columns like this

Code: Select all

$test=1,2,3,4,5,6,7,8,9
$datagridview1.Rows.Add($test)

But if it reaches a dozen columns, I need to write data in new rows in specific columns, such as 1 column, 3 columns, 9 columns, and no data in rows of other columns.

Code: Select all

$test=1,$null,3,$null,$null,$null,$null,$null,9
$datagridview1.Rows.Add($test)
It looks bad when written like this. I can't be sure which column name is written, so I need to count from 0-9 what data is written in how many columns one by one


If the column names are incremented and new columns are added or the order of the columns is adjusted, the order of the $test array needs to be reordered in order to write the newly added or reordered columns, which can become very troublesome in the debugging script


Is there a better way?

Similar to

Code: Select all

$datagridview1.Rows[0].Cells['Column1'].Value="1"
$datagridview1.Rows[0].Cells['Column2'].Value="3" 
$datagridview1.Rows[0].Cells['Column3'].Value="9"

This way I clearly know that I am writing data to Column1, Column2, Column3 column names
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: About the datagridview1.Rows.Add method of adding data to the specified column name row

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
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: About the datagridview1.Rows.Add method of adding data to the specified column name row

Post by jvierra »

Sorry but in this case, there is no way around writing code.

Note that it is not necessary to add quotes to numbers that are assigned to a string field. The field will do the conversion. Quotes are not wrong; they are just unnecessary.

I tend to always use a DataTable as it is more flexible.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: About the datagridview1.Rows.Add method of adding data to the specified column name row

Post by jvierra »

Fast generate a DataTable, Columns too. Bind to DGV.
  1. $dt = [System.Data.DataTable]::New()
  2. $c = [System.Data.DataColumn[]]@('Column1','Column2','Column3')
  3. $dt.Columns.AddRange($c)
  4. $datagridview1.DataSource = $dt
Create a new row and assign needed columns.
  1. $row = $dt.Rows.Add()
  2. $row['column1'] = 33
  3. $row['column3'] = 'Hello World'

Table now has sortable data and columns in DGV are auto-generated.
  1. PS C:\scripts> $dt
  2.  
  3. Column1 Column2 Column3
  4. ------- ------- -------
  5.  
  6. 33              Hello World
This topic is 1 year and 5 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