Accessing Objects Bound to Windows Forms DataGridView Rows

Accessing Objects Bound to Windows Forms DataGridView Rows

Added by Nasir Mahmood updated on Thursday, March 1, 2018

Problem:

Accessing data-bound objects directly through row objects is useful when you do not have direct access to the data source containing the objects.

In this Snippet we will learn how we can access object bound to DataGridView Row. DataGridViewRow.DataBoundItem Property is the key by this property we can get our custom object quite easily.

Getting DataRow From Row:

Assuming we bound an ordinary DataTable and DataGridViewRow is current row.

DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row				
				

Getting Object Bound From Row:

Assuming we bound List of Customer objects to DataGridView and DataGridViewRow is current row.

Customer cust = DataGridViewRow.DataBoundItem as Customer;				
				

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected