Datagridview select column

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 10 years and 2 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
mattalter
Posts: 23
Last visit: Thu Apr 03, 2014 8:56 am

Datagridview select column

Post by mattalter »

How would I go about selecting just the selected row in a datagrid view but only get the data from the second column?

Thanks
Matt
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview select column

Post by jvierra »

The columns collection is an array on the selected row. Just index into the column you want.

PowerShell Code
Double-click the code block to select all.
$button1_Click={
    $rowIndex=$datagridview1.CurrentCell.RowIndex
    [void][System.Windows.Forms.MessageBox]::Show($datagridview1.Rows[$rowIndex].Cells[1].Value,'Cell 2 value')
}

$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
    $rowIndex=$datagridview1.CurrentCell.RowIndex
    [void][System.Windows.Forms.MessageBox]::Show($datagridview1.Rows[$rowIndex].Cells[1].Value,'Cell 2 value')
}
This topic is 10 years and 2 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