DataGridView: Value of first Cell in Selected 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 9 years and 10 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
Eurisko
Posts: 17
Last visit: Fri Jun 16, 2023 11:06 am

DataGridView: Value of first Cell in Selected Row

Post by Eurisko »

I searched for this, but didn't find anything in the first 15 pages of search results. :)

I have a DataGridView that I have setup so when you click, it selects the entire Row.

How do I get the value to a string of the FIRST cell in the row that is selected?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView: Value of first Cell in Selected Row

Post by jvierra »

PowerShell Code
Double-click the code block to select all.
$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    $cell=$datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value
    [void][System.Windows.Forms.MessageBox]::Show($cell,"test")
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView: Value of first Cell in Selected Row

Post by jvierra »

Here is the same thing in the row changed event:
PowerShell Code
Double-click the code block to select all.
$datagridview1_SelectionChanged={
    $r=$datagridview1.SelectedRows[0].Cells[0].Value
    [void][System.Windows.Forms.MessageBox]::Show($r,"test")
}
User avatar
Eurisko
Posts: 17
Last visit: Fri Jun 16, 2023 11:06 am

Re: DataGridView: Value of first Cell in Selected Row

Post by Eurisko »

Unfortunately that only gives me the cell clicked on in the row. I actually need the first cell in the row, regardless of the cell clicked in the row. Does that make sense?

EDIT: Actually, the second option you listed did the trick. Thanks!
This topic is 9 years and 10 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