Mathematics
Mathematics

Reputation: 7628

Why we use Bound field in GridViews

I am pretty shocked no one asked this question before as when I searched I couldn't anything related,

https://stackoverflow.com/search?q=what+is+a+bound+field+c%23

Anyway my question is,

Can someone explain what a bound field is please in easy words and when we use it with example.

Research I did

We use it in a GridView or DataView but why we can't use the default option for displaying data then using Bound Fields.

Upvotes: 1

Views: 12273

Answers (2)

Neil Barnwell
Neil Barnwell

Reputation: 42155

Well Data Binding in general is the principle of declaratively stating that some user interface element's value will come from some source, and be populated by the runtime rather than the developer manually setting and getting values from controls in codebehind files.

So in WPF, for example, you can set the DataContext property of an entire window to an object, and then for each control on that window say from which properties of that object the WPF runtime should get their value.

For example, for an Employee viewmodel with Forename and Surname properties, you might create an EmployeeView window with two textboxes, where one is "bound" to the Forename property and the other is "bound" to the Surname property. At runtime, the framework will look at the bindings on each control, fetch the value from the data automatically and populate the control's value field. Likewise, when the value in the control is modified by the user, data-binding can push the new value to the data model it is bound to.

This is in contrast to the typical approach in the days of VB6, where setting those textboxes' content would be done in the codebehind of the form (e.g. forenameTextBox.Text = employee.Forename). Data binding in VB6 (and WinForms, for that matter) is different, where the framework does what I described above, but automates getting data from a database in the process. That's fallen out of favour in recent years, though (and for good reason).

Upvotes: 3

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

The BoundField class is used by data-bound controls (such as GridView and DetailsView) to display the value of a field as text. The BoundField object is displayed differently depending on the data-bound control in which it is used. For example, the GridView control displays a BoundField object as a column, while the DetailsView control displays it as a row.

For more visit MSDN Help Bound Field Description

Upvotes: 1

Related Questions