When working with ASP.NET GridView, you might sometimes notice a weird behavior β€” the OnRowDataBound event fires for the header rows, but not for the actual data rows. Everything looks fine visually, yet your event logic never runs for the records.

I faced this problem recently and learned a few valuable lessons along the way. Here’s what happened and how I solved it.


🧩 The Situation

I had a GridView with a few fields defined manually like this:

<asp:GridView ID="MyGridView"
    runat="server"
    AutoGenerateColumns="False"
    OnRowDataBound="MyGridView_RowDataBound">
    <asp:HyperLinkField DataTextField="Name" />
    <asp:BoundField DataField="Email" />
</asp:GridView>

And the event handler looked simple enough:

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Apply formatting or logic here
    }
}

But when I ran the page β€” the event fired only for the header, not for any of the data rows. No errors, no visible clues.


🧠 The Root Cause

After investigating, it turned out that an internal binding exception was being thrown and swallowed during the GridView’s data-binding phase.

That silent exception prevented the control from finishing its normal binding cycle, so the RowDataBound event never triggered for data rows.

Once the exception was fixed, the event worked perfectly for every row as expected.


πŸ’‘ My Simple Fix

In my own case, the issue appeared when I was using a HyperLinkField.

Surprisingly, switching to a ButtonField immediately solved it:

<asp:ButtonField Text="Click" CommandName="Action" />

After replacing the HyperLinkField, the OnRowDataBound event started firing for all rows again.

Sometimes GridView columns can behave slightly differently internally, so this swap can bypass certain binding quirks.


πŸͺ„ Quick Tips

  1. Always wrap your data-binding code in try-catch and check for hidden exceptions.
  2. Avoid overusing HyperLinkField when a ButtonField or TemplateField gives more control.
  3. If OnRowDataBound runs only for headers, check your data source and ensure that DataBind() completes without silent failures.

βœ… Final Thoughts

The OnRowDataBound event is extremely useful for formatting rows, injecting controls, or running per-row logic. But when it only triggers for headers, it’s usually a binding problem or a field type issue.

In my experience, using a ButtonField instead of a HyperLinkField solved the issue completely β€” a quick, practical workaround that might save you hours of debugging.


Author: AlgoLassi Tech Blog
Topic: ASP.NET Tips
Tags: asp.net, gridview, rowdatabound, csharp

πŸ€– AlgoLassi Assistant Have a question about this tutorial?

Ask AlgoLassi and get an answer plus the tutorials worth studying next.

Ask a question

πŸ’¬ Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.