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
- Always wrap your data-binding code in
try-catchand check for hidden exceptions. - Avoid overusing
HyperLinkFieldwhen aButtonFieldorTemplateFieldgives more control. - If
OnRowDataBoundruns only for headers, check your data source and ensure thatDataBind()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
Ask AlgoLassi and get an answer plus the tutorials worth studying next.
π¬ Comments
Sign in with Google to publish immediately, or comment anonymously and wait for approval.
Comments will appear here when available.