DataGridView - How to select header without selecting Cell

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.
Locked
zsirkin
Posts: 11
Last visit: Fri Dec 03, 2021 5:10 pm

DataGridView - How to select header without selecting Cell

Post by zsirkin »

Hi I have what I hope is a very simple question.

How can I select (sort) a header column without selecting a (pre-selected) cell.

Basically, by default Cell 0,0 is selected. Because I have the following for "$Datagridview1_CellClick":

Code: Select all

		if ($datagridview1.CurrentCell.ColumnIndex -eq 0)
		{
			Update-ListBox -ListBox $listbox1 -Items "$($datagridview1.CurrentCell.Value)" -Append
		}
I end up not being able to perform any sorting anywhere. I've tried using ($datagridview1.CurrentCell.RowIndex -eq "-1") to ignore the previous code, but in reality, the only thing the code sees is the highlighted cell, not the selected header.

What am I doing wrong? Thanks!

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: DataGridView - How to select header without selecting Cell

Post by jvierra »

No idea what you are trying to ask. A column is sorted by clicking on the header and that is all that is necessary.

YOu are starting a very bad habit by arbitrarily adding quotes around everything fo no reason. Start by removing all quotes except if it is a typd in string in the editor. PowerShell is an object system and knows how to work with objects. Quotes are overriding the overall behavior of the object system.

zsirkin
Posts: 11
Last visit: Fri Dec 03, 2021 5:10 pm

Re: DataGridView - How to select header without selecting Cell

Post by zsirkin »

The quotes are there because I would typically be adding more than just a variable to an Update-Listbox (also why i've protected the variable) and this is a simple demo to do other work. I'm not new to this, it's just the first time working with DataGridView...

As I stated, this issue occurs when I use the Event "CellClick".

When the form loads, cell 0,0 is already highlighted by default.

The primary goal is to select the appropriate cell as long as it's in column 0 (first part of the code you see).

Now if I want to sort prior to clicking a cell, the event CellClick still triggers. SO if I click a header to sort, it still clicks the highlighted cell automatically. This is what I would like to keep from happening.

zsirkin
Posts: 11
Last visit: Fri Dec 03, 2021 5:10 pm

Re: DataGridView - How to select header without selecting Cell

Post by zsirkin »

Visual has been added.

Button1 triggers 3 columns of data. First I selected the cell "C0-R1". This displays in info below. Then I selected the header "Column0", this once again recognized that a cell was already selected and triggered the cell click. If I click any other column, nothing of course happens due to the code I wrote above.

Even if I click button1 and never select a cell, "C0-R0" is automatically triggered and displayed when I perform a "column0" header click.
dgv.PNG
dgv.PNG (16.57 KiB) Viewed 4170 times

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: DataGridView - How to select header without selecting Cell

Post by jvierra »

It still doesn't work as you think it does. Quotes alter objects when the are repointed because you are creating a new object and that is not what you want.

I recommend learning PowerShell and object systems as PowerShell is an object system because the Net Framework is an object system.

If you click any cell the cell click event will always occur and you can't change that.

The header is also a cell and sends the cell click event. Detect the cell and check if it is a header cell. This has absolutely nothing to do with the first cell being hiligted, Sorting will reset the top cell and hilight it.

Again you need to learn object systems because forms are also an object system and in a collection of collections that are stacked up to a form then the actions represented by clicks effect the whole hierarchy of collections so it is necessary to understand the "controls" hierarchy.

The simple answer is to detect the cell that has been clicked and ignore the cell if it is a header cell.

I also sense that you may be trying to maintain a certain cells hilight so you have to grab the hilighted cell before the sort happens and save the cell, then after the sort, hilight the cell that has been saved.

zsirkin
Posts: 11
Last visit: Fri Dec 03, 2021 5:10 pm

Re: DataGridView - How to select header without selecting Cell

Post by zsirkin »

See the images below, one with quotes and one without. This is not my issue, nor do I care about that. I've got a 50k+ line production level Sapien built application that is used by a ton of administrators without fault yet you're assuming I can't build a simple form. I have what I'm assuming is a simple issue with a control that I'm unfamiliar with, and I'm just asking for a handout. I've tried to identify the header as currentcell.rowindex -eq -1, but that doesn't work either. If you know the answer, please share it.
Thanks

Error.PNG
Error.PNG (70.55 KiB) Viewed 4162 times
NoError.PNG
NoError.PNG (56.49 KiB) Viewed 4162 times

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: DataGridView - How to select header without selecting Cell

Post by jvierra »

Have you read the docs on the cell object, row object, row collection and the click events associated with the header?

YOu can't expect anyone to remeber deep detail when controls in WInforms have 10,000+ events and thousands of properties and object collections.

Always start by reading the documentation. I skim the controls methods, events and properties just get oriented to the control and how behaves. This is why I suggested learning the object model for WinFOrms.

So I did what you should have done first. I looked up the DGV even though I have used it a thousand times. The issue would have been obvious if you looked up the control and tried to understand how it works.

I am not trying to give you a hard time. I am trying to show you how we programmers and software engineers proceed with any new system that we have to work with. We always start by reviewing the documentation. I have learnhed hundreds of systems because I have learned to read the documentation. It has saved me, likely, tens of thousands of hours over guessing and copying and fudging around.

All you have to do is right click on hte control in the toolbox and select "MSDN Help" on the menu, which will take you to the documentation for the selected control.

Also consider that your question was arbitrary which it wouldn't have been if you had read the document on the control. You can't expect people you ask to do all of the work, after alll, it is your project.

Here is one hint at the first step that you can try to do whatever it is you want to accomplish which is still quite vague in my opinion.
  1. $datagridview1_ColumnHeaderMouseClick=[System.Windows.Forms.DataGridViewCellMouseEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
  3.    
  4. }

Locked