Data Binding: Grid View, List Box, Data binding Events, Repeater, Form View

Data binding connects a collection of data to a control so it displays every item automatically: hand one list of events to a GridView, ListBox, Repeater or FormView and it renders all the rows for you, no manual HTML per item.

10 min read · 8 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

One list, many rows, no copy-paste

FestConnect has a list of events, maybe 30 of them, and each needs a row on screen: name, date, venue. You are not going to hand-write 30 blocks of HTML, and you certainly will not re-edit them every time the event list changes.

Instead you hand the whole list to a control and let it draw every row for you. That is data binding: connecting a source of data to a control that displays it. Change the underlying list and the display follows. This lesson covers data binding and the main controls that use it: GridView, ListBox, Repeater and FormView.

Theory

How data binding works

The pattern is always the same two steps. You set the control's DataSource to your data (a list, a DataTable, the results of a query), then you call DataBind(). The control walks through the data and renders one entry per item.

That is the whole idea: give it the data, tell it to bind, and it builds the display. If the data has 5 events, you get 5 rows; if it has 50, you get 50, with no change to your code. The control handles the repetition so you do not have to.

Practical

Binding the FestConnect event list to a GridView

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // 'events' could come from a database; a list is shown here for clarity
        var events = GetEvents();        // e.g. a list of Event objects

        gvEvents.DataSource = events;    // 1) point the control at the data
        gvEvents.DataBind();             // 2) render one row per item
    }
}
// gvEvents is an <asp:GridView> in the markup. It now shows every event.

At a glance

ControlShowsLayout control
GridViewA grid: rows and columns, like a tableBuilt in; supports sorting and paging
ListBoxA scrollable list of selectable itemsSimple list; user can pick
RepeaterWhatever HTML template you write, repeatedTotal: you supply the template, no default
FormViewOne record at a time, your templateOne item, fully custom layout

Formula

DataBind() and per-item events

Nothing appears until you call DataBind(); setting DataSource alone does not render anything. Remember to bind (typically inside if (!IsPostBack), so the grid is not needlessly rebuilt on every click).

As a control binds, it can raise data-binding events, one per item. A GridView's RowDataBound fires for each row as it is built, letting you customise it: for example, colour a 'Full' event red, or format a date. These events are your hook to adjust individual items during binding.

Quiz

You want to show FestConnect's events as a table with columns, sorting, and paging, with minimal effort. Which control fits best?

  1. Repeater, because it has the most built-in table features
  2. GridView, which renders rows and columns as a grid and supports sorting and paging out of the box
  3. FormView, because it shows all records as a grid
  4. ListBox, because it displays columns automatically
Show the answer

GridView, which renders rows and columns as a grid and supports sorting and paging out of the box

GridView is the tabular grid control: it renders rows and columns and has built-in sorting and paging, so it fits a table of events with least effort. Option A is backwards: the Repeater has NO built-in layout at all; you must write the entire template yourself, which is the opposite of minimal effort for a standard table. Option C is wrong because FormView shows ONE record at a time, not a grid of all of them. Option D is wrong: a ListBox is a single-column scrollable list of selectable items, not a multi-column grid. Match the control to the shape of the display: grid of many rows -> GridView.

Think first

So when would you pick a Repeater over a GridView?

If GridView does so much automatically, why does the Repeater, which gives you nothing by default, even exist? Then tap.

Show the answer

You choose a Repeater when you want COMPLETE control over the HTML and do not want the grid's built-in table structure. A GridView is quick but it renders a table with its own markup, which is great for a spreadsheet-like grid but limiting if you want, say, each event shown as a styled card, or a custom list of divs, or markup that fits a specific design. The Repeater renders exactly the template you write, once per item, and nothing else: no automatic table, no extra tags. That freedom is the point. The trade-off is effort and features: with a Repeater you write all the layout yourself and you do not get free sorting or paging. So the rule is: GridView for a standard data grid with least work; Repeater when the design demands custom markup and you are happy to write it. FormView is the same idea for a single record. Right tool, right shape.

Summary

Key takeaways

  • Data binding connects a data source to a control that displays every item automatically.
  • The pattern is two steps: set the control's DataSource, then call DataBind(); nothing renders until you bind.
  • GridView shows a grid of rows and columns with built-in sorting and paging.
  • ListBox is a scrollable list of selectable items; Repeater renders a fully custom template you write, per item; FormView shows one record at a time with a custom template.
  • Data-binding events like GridView's RowDataBound fire per item, letting you customise each row as it is built.
  • Bind inside if (!IsPostBack) so the control is not needlessly rebuilt on every postback.
  • Memory hook: DataSource + DataBind() = the control draws every row for you.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to ASP.NET

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Data Binding: Grid View, List Box, Data binding Events, Repeater, Form View · .NET Technology (Major-13) · Gri-Learn