How Do I Get the Selected DataRow in a DataGridView?

前端 未结 7 1107
我寻月下人不归
我寻月下人不归 2021-01-17 09:00

I have a DataTable bound to a DataGridView. I have FullRowSelect enabled in the DGV. Is there a way to get the selected row as a DataRow so that I can get strongly typed a

7条回答
  •  情深已故
    2021-01-17 09:01

    It is possible by getting following property:

    this.dataGridView.SelectedRows
    

    One obtains a collection of type: DataGridViewSelectedRowCollection. It contains items of type: DataGridViewRow.

    Then one can get bounditem with ones own type in following way:

    DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows;
    MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item
    

提交回复
热议问题