Page 1 of 1

DataGridView - when I search multiple times, the first cell data is blank

Posted: Thu May 02, 2019 12:44 pm
by kdwisdom
To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2019 version 5.6.162
32 or 64 bit version of product: 64
Operating system: Windows 10
32 or 64 bit OS: 64

*** Please add details and screenshots as needed below. ***

I got PS Studio for my job a couple of weeks ago and I LOVE IT! It is wonderful. It was a very easy transition moving from C# WinForms to PS WinForms.

I'm running into my first snag though today.

I have a few forms in my multi-form program that I am using DataGridViews for. Most of them are 1 column wide, so I am just manually adding to that DGV from a foreach-object loop to populate it.

However, I'm working on one that is multi-column to show user names and access types for e-mail permissions (Connects to Exchange, tells you who has what kind of access to that mailbox).

For instance,
You would search for mailbox1@abc.com
It would return

Name|AccessType
John | FullAccess
Mary | FullAccess

That's all good and fine. HOWEVER... If I type a different e-mail address and click search again, this is what happens:

Name|AccessType
| FullAccess
Jack | FullAccess

It is erasing the first cell for some reason on that reload/new search.

Here is the code I currently have:
  1. $currentMailbox = $emailAddress.Text
  2.     $getRights = get-mailbox $currentMailbox | get-mailboxpermission | ? { ($_.User -ne 'NT AUTHORITY\SELF') -and ($_.IsInherited -eq $false) } | select @{ Name = "User Name"; expression = { (Get-Recipient $_.user.tostring()).displayname } }, accessrights
  3. Update-DataGridView -DataGridView $datagridviewResults -Item $getRights #-AutoSizeColumns DisplayedCells
Does anyone know how to fix that issue?

Re: DataGridView - when I search multiple times, the first cell data is blank

Posted: Fri May 03, 2019 5:18 am
by mxtrinidad
If the purpose of the Datagridveiw is to display the information, so it can't be altered. In the control property panel, under "Behavior", change the ReadOnly property of the control from False to True.

This will protect the cell from been overwritten.

:)

Re: DataGridView - when I search multiple times, the first cell data is blank

Posted: Fri May 03, 2019 8:39 am
by kdwisdom
I thought that would be the case as well. I had already set it to read only and it still overwrites the cell :/

Re: DataGridView - when I search multiple times, the first cell data is blank

Posted: Fri May 03, 2019 11:33 am
by mxtrinidad
One thing for sure, the issue seems not in the code you provided. It's somewhere in the property of the form.

Because we don't have the form, why not try using one of the templates name "Grid". Then, in the "$buttonLoad_Click", replace the code with yours and seen if it still happens.

I did a test by un-commenting the code behind the "$buttonLoad_Click" event and seem to work fine.

Give it try and let us know!

:)

Re: DataGridView - when I search multiple times, the first cell data is blank

Posted: Fri May 03, 2019 2:18 pm
by kdwisdom
mxtrinidad wrote: Fri May 03, 2019 11:33 am One thing for sure, the issue seems not in the code you provided. It's somewhere in the property of the form.

Because we don't have the form, why not try using one of the templates name "Grid". Then, in the "$buttonLoad_Click", replace the code with yours and seen if it still happens.

I did a test by un-commenting the code behind the "$buttonLoad_Click" event and seem to work fine.

Give it try and let us know!

:)
Ok, I discovered 2 issues with this.

1. I was running this:
  1. $getRights = Get-Mailbox $currentMailbox | get-mailboxpermission | ? { ($_.User -ne 'NT AUTHORITY\SELF') -and ($_.IsInherited -eq $false) <#-and ($_.User -notlike '*DOL*')#> } | select @{ Name = "User Name"; expression = { (Get-Recipient $_.user.tostring()).displayname } }, accessrights
Running this gives an empty field, even in PowerShell, not just the datagridview.


Running this, on the other hand:
  1. $getRights = get-mailboxpermission $currentMailbox | ? { ($_.User -ne 'NT AUTHORITY\SELF') -and ($_.IsInherited -eq $false) -and ($_.User -notlike '*DOL*') } | select @{ Name = "User Name"; expression = { (Get-Recipient $_.user.tostring()).displayname } }, accessrights
That bypasses the "Get-Mailbox" all together since we already have the address. The problem was that when I piped get-mailboxpermission after get-mailbox, it doesn't return data the same but running just straight get-mailboxpermissions $currentMailbox and then the rest of the command, it returns the right data every time.


2: One of the e-mail addresses I was querying had no displayname, so it was showing blank. Even so, it shouldn't have been erasing cell 1. That problem was resolved from not piping the data.

Re: DataGridView - when I search multiple times, the first cell data is blank

Posted: Fri May 03, 2019 2:57 pm
by mxtrinidad
Good!

It's all about understanding the object, and some times make us go in circle.
Glad you resolved the issue!

:)