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.
Locked
User avatar
Eurisko
Posts: 17
Joined: Thu Feb 14, 2013 9: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: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

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: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

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
Joined: Thu Feb 14, 2013 9: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!

Locked