![]() |
index number of a datarow in a data table
Hi All:
Here´s a very simple question: I have created a datatable inside a dataset, and subsequently selected a particular row using certain criteria. How do get the index number of that particular row? There doesn´t seem to be property like "mydatable.currentlyselectedrowindex" or "myrow.indexnumber". Any suggestions will be highly appreciated. TB |
RE: index number of a datarow in a data table
You probably should not build a program that depends on the row index.
There is not an index property for the DataRow because a program does not usually depend on an index that would change every time the collection is resorted or re-filtered. If, for whatever reason, you have to get the row index you might have to iterate through the rows collection till you find the a row that equals the row you have selected, for example: Dim foundRow As DataRow ' Create an array for the key values to find. Dim findTheseVals(2) As Object ' Set the values of the keys to find findTheseVals(0) = "John" findTheseVals(1) = "Smith" findTheseVals(2) = "5 Main St." foundRow = myTable.Rows.Find(findTheseVals) Dim i As Integer Dim iRowIndex As Integer For i = 0 To myTable.Rows.Count - 1 If myTable.Rows(i).Equals(foundRow) Then iRowIndex = i Exit For End If Next -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "TB" wrote: > Hi All: > > Here´s a very simple question: > > I have created a datatable inside a dataset, and subsequently selected > a particular row using certain criteria. How do get the index number of > that particular row? There doesn´t seem to be property like > "mydatable.currentlyselectedrowindex" or "myrow.indexnumber". > > Any suggestions will be highly appreciated. > > TB > > |
Re: index number of a datarow in a data table
Thanks for replying so fast.
You see, my requirements are the following: I need to create a page which has two functions: 1) display the content a specific record in a database table and 2) page forward and backward from the initial specific record mentioned before. When the page loads, a datatable ("mydatatable") is created, and a specific record is selected according certain criteria. Furthermore, the page contains "Next" and "Previous" buttons, which allows for the user to move forward and backwards in the datatable, but always using the initially selected record as a starting point. My idea is therefore to identify the initially selected record by its index number, from which one can more up and down by adding to or subtracting from that initial value (the minimum value being 0 and the maximum value being mydatatable.rows.count - 1) Let me know what you think. Thanks TB |
Re: index number of a datarow in a data table
You might be able to avoid searching for the row index if you bind the data
to server controls (which have index for selected records). http://www.webswapp.com/CodeSamples/...FormView2.aspx I put at this link a demo where I bound a dropdownlist and a FormView to do the function that you are trying to do without writing codebehind to search for the row index. -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "TB" wrote: > Thanks for replying so fast. > > You see, my requirements are the following: > > I need to create a page which has two functions: 1) display the content > a specific record in a database table and 2) page forward and backward > from the initial specific record mentioned before. > > When the page loads, a datatable ("mydatatable") is created, and a > specific record is selected according certain criteria. Furthermore, > the page contains "Next" and "Previous" buttons, which allows for the > user to move forward and backwards in the datatable, but always using > the initially selected record as a starting point. > > My idea is therefore to identify the initially selected record by its > index number, from which one can more up and down by adding to or > subtracting from that initial value (the minimum value being 0 and the > maximum value being mydatatable.rows.count - 1) > > Let me know what you think. > > Thanks > > TB > > |
Re: index number of a datarow in a data table
You seem to use ASP.NET 2.0 for your demo. Since I am still in the 1.1x
world, I wonder whether your example will work. Furthermore, I "only" understand VB.NET, and not C#. Please bear with me..... TB |
Re: index number of a datarow in a data table
In ver#1.1, you can use a datagrid with PageSize=1 to get the equivalent of a
FormView. The rest is the same. Here is a demo in VB: http://www.societopia.net/samples/record_select.aspx -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "TB" wrote: > You seem to use ASP.NET 2.0 for your demo. Since I am still in the 1.1x > world, I wonder whether your example will work. Furthermore, I "only" > understand VB.NET, and not C#. > > Please bear with me..... > > TB > > |
Re: index number of a datarow in a data table
Thanks a lot for your insight.
Going back to your initial proposal, I wondered if it could simplified a bit, because it seems like it runs through the entire mytable twice (first at the "mytable.Rows.Find(findthesevals) and later in the For.....Next loop. How about something like this (in this case inside a page_load sub): Dim foundRow As DataRow Dim irecord as integer Dim i As Integer Dim iRowIndex As Integer Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then 'The first time the page is accessed Session("irecord") = Request.QueryString("id") 'Just for the sake of this example For i = 0 To myTable.Rows.Count - 1 If myTable.Rows(i).item("ID") = irecord Then foundrow = mytable.rows(i) iRowIndex = i Session("iRowIndex") = irowIndex Exit For End If Next i Else irecord = Session("irecord") End If filltable() 'referrring to a sub where the table is populated with foundrow content End Sub On the page there could be two buttons for navigating back and forward. This would be the associated code: Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click if irowIndex <> 0 then Session("irowIndex") = Session("irowIndex") - 1 foundrow = mytable.Rows(Session("irowIndex")) filltable() end sub Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click if irowIndex <> (mytable.Rows.Count - 1) then Session("irowIndex") = Session("irowIndex") + 1 foundrow = mytable.Rows(Session("irowIndex")) filltable() end sub Please let me know what you think. |
Re: index number of a datarow in a data table
you can get index of a datarow in datatable as follows:
objDataTable.Rows.IndexOf(objDataRow) |
allAboutMe
Any body who tells you you don't need to be able to get the index of a DataRow or DataRowView lives in fairly land.
We need a method for DataViews. The Rows.IndexOf method won't work if you what the index for a DataRowView and its not part of the default view. This has been a problem ever since the first version of ADO.Net. The dataTable Rows.IndexOf method wasn't introduced until .Net Framework Version 2.0 . Come on Microsoft get real and make it simple. (Complexity & Dependencies aren't the part of the solution.) |
| All times are GMT. The time now is 03:20 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.