How to get the data "under" the selected row in datagriedview binded to datatable

All that you must do is to create an instance of CurrencyManager class. This class primary work is to manage list of binding objects. So to get the data under the selected row we must supply the DataSource and the DataMember of the DataGridView. DataMember represents the current list or the current table that are displayed. By supplying:


// ...
BindingContext[dgvDataView.DataSource, dgvDataView.DataMember];
// and casting to CurrencyManager
// ...


we create an instance of class CurrencyManager which derives from the abstract class BindingManagerBase.

By supplying



//...
currManager.Current
//...



we get the current item in a object type. The only thing that we have to do is to cast this object to the appropriate type. In our case DataViewRow. So here is a method returning currently selected row in DataGridView.



public static DataRow GetCurrentRow(DataGridView dgvDataView)
{
CurrencyManager currManager =
(CurrencyManager)dgvDataView.BindingContext[dgvDataView.DataSource, dgvDataView.DataMember];
DataRowView drView = (DataRowView)currManager.Current;

return drView.Row;
}

0 коментара:

Публикуване на коментар

top