DataGridView and other subitems

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 11 years and 3 weeks 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
arkainus
Posts: 14
Last visit: Mon Sep 07, 2015 12:45 am

DataGridView and other subitems

Post by arkainus »

Hi,

I'm working on a form that I created using Primal Forms Community Edition. It is a multi-tabbed form, and I added a DataGridView control to the last tab. I have that go against a Database on a server, to allow for pulling, and pushing data back to my SQL server. However, I want one cell to be act as a combobox, and one to act sort of like a masked text box if at all possible.

I was able to get the ComboBox working... sort of. My code is below:
PowerShell Code
Double-click the code block to select all.
#endregion Generated Form Objects
$dgvcbc = New-Object System.Windows.Forms.DataGridViewComboBoxCell
$bindingSource1 = new-object System.Windows.Forms.BindingSource
$dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$serverName = “myserver.mydomain”
$databaseName = “MyDB”
$query = ‘select * FROM tblPotConflicts’
#———————————————-
#Generated Event Script Blocks
#———————————————-
#Provide Custom Code for events specified in PrimalForms.
$Form1_Load=
{
#$dataGridView1.AutoGenerateColumns = $True
$dataGridView1.DataSource = $bindingSource1
$connString = “Server=$serverName;Database=$databaseName;Integrated Security=SSPI;”
$dataAdapter.SelectCommand = new-object System.Data.SqlClient.SqlCommand ($query,$connString)
$commandBuilder = new-object System.Data.SqlClient.SqlCommandBuilder $dataAdapter
$dt = New-Object System.Data.DataTable
[void]$dataAdapter.fill($dt)

$bindingSource1.DataSource = $dt
$a = "Watty", "Secretary", "Client"
$dgvcbc.DataSource = $a
$dataGridView1.Columns.Add($dgvcbc)

$dataGridView1.AutoResizeColumns([System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::AllCellsExceptHeader)
}
This code will create the bindings for the DataGridView and populate the columns/values from the DataBase, appending the ComboBoxCell at the end of the table, and populate it with the values stored in the array $a for choice. However, there is no header text on the column. In C#, you could choose the name of the column via a simple dgvcbc.HeaderText = "Name" statement. There is no such property in the PowerShell implementation though. Anyone know of a way to set the name? Also, does anyone know of a way to set the DefaultNewRowValue? There is a property when I use Get-Member, but the only method is get, no set method.

Finally, has anyone worked out a way to add a sort of masked text box cell within PowerShell and the DataGridView? In the MSDN windows form samples, the DataGridViewCustomColumn solution works for C#, but I have no idea how to implement in PowerShell. My users are lazy and in a telephone field I know we will have all sorts of inconsistencies if I don't mask the actual input from them ((555) 555-5555 vs 555-555-5555, etc.). I have to work within the DataGridView because the data will be variable - i.e. sometimes there will be 0 conflicts, sometimes there can be as many as 50, so just creating a static form with 50 of the plain ol' masked text box controls (and all of the other fields I'm gathering info for in the DataGridView currently), I don't think will cut it (though, I'm willing to listen if someone much smarter than me has any suggestions ;) ).

Edit - Got the Header to work by changing the object type of dgvcbc to DataGridViewComboBoxColumn from DataGridViewComboBoxCell. That allows me to set the HeaderText. Hopefully someone has some insight on my other issues.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView and other subitems

Post by jvierra »

Coumn herder for a COmbo column:
PowerShell Code
Double-click the code block to select all.
#
# Column1
#
$Column1.HeaderText = "Column1"
$Column1.Name = "Column1"
#endregion Generated Form Code
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView and other subitems

Post by jvierra »

If you have the C# code that creates a GridView custom column then it can be loaded into PowerShell. If you don't have the code you will have to write it.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView and other subitems

Post by davidc »

You can use the designer to specify the columns you want before hand. There you can set the HeaderText and the DataPropertyName.

Has for formatting the phone numbers, you can use the DataGridView's CellFormating Event:
PowerShell Code
Double-click the code block to select all.
$datagridviewResults_CellFormatting=[System.Windows.Forms.DataGridViewCellFormattingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellFormattingEventArgs]

	if($datagridviewResults.Columns[$_.ColumnIndex].Name -eq "Phone")
	{
        if ($_.Value -ne $null)
        {
			$_.Value = Format-Phone $_.Value.ToString()
            $_.FormattingApplied = $true
		}
	}
}
David
David
SAPIEN Technologies, Inc.
User avatar
arkainus
Posts: 14
Last visit: Mon Sep 07, 2015 12:45 am

Re: DataGridView and other subitems

Post by arkainus »

davidc wrote:You can use the designer to specify the columns you want before hand. There you can set the HeaderText and the DataPropertyName.

Has for formatting the phone numbers, you can use the DataGridView's CellFormating Event:
PowerShell Code
Double-click the code block to select all.
$datagridviewResults_CellFormatting=[System.Windows.Forms.DataGridViewCellFormattingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellFormattingEventArgs]

	if($datagridviewResults.Columns[$_.ColumnIndex].Name -eq "Phone")
	{
        if ($_.Value -ne $null)
        {
			$_.Value = Format-Phone $_.Value.ToString()
            $_.FormattingApplied = $true
		}
	}
}
David

This seems to be on the right track of what I want. At first it was telling me I couldn't index into a Null array so I changed your code to the following:
PowerShell Code
Double-click the code block to select all.
$datagridviewResults_CellFormatting=[System.Windows.Forms.DataGridViewCellFormattingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellFormattingEventArgs]
 
    if($datagridview.Columns[$_.ColumnIndex].Name -eq "Phone")
    {
        if ($_.Value -ne $null)
        {
            $_.Value = Format-Phone $_.Value.ToString()
            $_.FormattingApplied = $true
        }
    }
}
Now I am getting an error of:

Format-Phone : The term 'Format-Phone' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At C:\Users\clokey\Desktop\client - Copy.ps1:216 char:24
+ $_.Value = Format-Phone $_.Value.ToString()
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Format-Phone:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Is this a cmdlet that is included in PowerShell 3.0? I am using PowerShell 2.0 in my environment currently? If it is a function you use heavily, would you perhap mind sharing that as well? Thank you for your help!! :)
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView and other subitems

Post by davidc »

Format-Phone doesn't exist :) I was just an example of where you should put your code. You will have to parse the number depending on how it is formatted in the database.

The easiest way will probably be to remove any non-numeric symbols such as ()- and spaces. Then you can work with the numbers only and format the phone number however you like.

David
David
SAPIEN Technologies, Inc.
User avatar
arkainus
Posts: 14
Last visit: Mon Sep 07, 2015 12:45 am

Re: DataGridView and other subitems

Post by arkainus »

davidc wrote:Format-Phone doesn't exist :) I was just an example of where you should put your code. You will have to parse the number depending on how it is formatted in the database.

The easiest way will probably be to remove any non-numeric symbols such as ()- and spaces. Then you can work with the numbers only and format the phone number however you like.

David
10-4, I will give that a try. Thank you!
This topic is 11 years and 3 weeks 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