Ordinal row positions in DataGrid/DataSet

Posted on 2005-05-17
Last Modified: 2010-04-07

I don't see properties for either the datagrid or dataset that indicates how to get .Count or ordinal position.

I'm binding a DataSet to a DataGrid and need to start working with the ordinal row positions in a couple differnt contexts. For example, i have a column holding numeric values and want to find which row number a given value is found in. And then I'd want to know total number of rows. Then I'd be able to produce, for example, "Row 3 of 40" output.

Also, and this is probably more complex...I have a templete column that holds links to script that 'moves up' and 'moves down'. The script will update the chosen record so the id field is incremented or decrimented.

    '<a href="#" onclick="javascript:popup('RowEdit.aspx?theAction=moveup&myID=<%# DataBinder.Eval(Container, "DataItem.myIdentity") %>', 'EditRow','scrollbars=yes,resizable=yes,width=600,height=300')">Move Up</a><br>
    '<a href="#" onclick="javascript:popup('RowEdit.aspx?theAction=movedown&myID=<%# DataBinder.Eval(Container, "DataItem.myIdentity") %>', 'EditRow','scrollbars=yes,resizable=yes,width=600,height=300')">Move Down</a><br>

I need a way to disable (or prefereably not display) the 'move up' link when outputting the first row of the datagrid - likewise disable the 'move down' link when rendering the bottom link.

Understanding that this may not be possible to proccess in the HTML portion of the page I'd need to see how to obtain those values and then append them to the querystring being constructed the same way I'm appending the 'myIdentity' field from the databinder.

Question by:juststeve
    LVL 33

    Accepted Solution

    Look into the ItemDataBound or ItemCreated Events the DataGrid Fires.  Each "row" in the datagrid has an index, that counts up, which you can easily get at in this sub.

    e.Item.ItemIndex (the row # would be e.Item.ItemIndex + 1)

    also, after the datagrid is bound, you can get a count of the rows using

    I see no reason why you can't get what you are keep at it! :-)
    LVL 23

    Assisted Solution

    The ItemIndex is good as long as you are aware of the following gotcha. The underlying data source in this case is the DataView and not DataTable. The individual "row" item is the DataRowView and not the DataRow. There's a difference here in that the DataView may show a subset of the DataTable.

    Author Comment

    Couldn't seen the reference to index cuz I was looking for it in the toplevel of the members list helpfile. Next time I'll know to browse thro some likely links.

    thankx to both...shouldda added and slit time b1 (& i think we can all count on there being a 'next time').
    LVL 23

    Expert Comment

    whoa! so Page Editors can also reopen questions hmmmm!!!
    LVL 23

    Expert Comment

    too much responsibility... =))

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now