DataGrid Row Numbering On Empty Reader

E

Elroyskimms

I have a DataGrid and the first column is a template column which
displays 1 for the first row, 2 for the second row, 3 for the third
row, and so on. I accomplish this by using the following tag inside the
template column:

<%# Container.ItemIndex + 1 %>

If the DataSource is a datareader with at least one record, everything
works great. If the DataSource returns a datareader that has no rows,
the DataBind command errors out, claiming that it is trying to access a
reader that is closed. Apparently the ItemIndex property is still being
accessed (or at least trying to be accessed) even though the datareader
is empty (not closed, just empty).

If I remove the "numbering" template column, the problem goes away. If
I trap the DataBind line of code within a Try Catch block, the problem
goes away. Is there a way to accomplish the "numbering" column in a
datagrid that can also gracefully handle an empty datareader?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top