DataGridView: Value of first cell in Selected Row ToString

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Barsby Dave Barsby 3 years, 1 month ago.

  • Author
  • #14919
    Profile photo of Troy Helms
    Troy Helms

    I have a DataGridView that allows the user to click on any row, and it selects the entire row as desired. However, I'm having an issue getting he value of the FIRST Cell in that Row that is selected to a string value.

    Previously when I only had 1 column being listed, it was easy:
    $SelectedRealm = $ProdRealmsdatagridview.CurrentCell.Value.ToString()

    Any help is appreciated.

  • #14932
    Profile photo of Dave Barsby
    Dave Barsby

    This is how I select values from a DataGridView

    $datagridview1_CellContentClick = [System.Windows.Forms.DataGridViewCellEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    write-host $_.RowIndex # This displays the row number selected
    write-host $_.ColumnIndex # This displays the column number selected
    write-host $datagridview1.Rows[$_.RowIndex].Cells[0].value # This would display the value of the first cell in the selected row
    write-host $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].value # This would display the value of the cell selected

    I hope that helps

You must be logged in to reply to this topic.