6
Answers

It shouldn't be this tough. Finding records

Administrator

Administrator

22y
5.4k
1
OK, I need to find the index of a specific row in a dataset based on field value criteria, such as social security number. I've tried using a dataview and the Find() method, but that requires that you SORT on the column that your are searching on first. That's great, but it returns and index that does not match the index of the original DataTable that I am searching. This is not at all beneficial. I could use the DataTable.Rows.Find method as well, but that requires that one search on the Primary Key. This is no use to me. I simpky want to be able to search on a specific column and get the row index that it finds the value in. Am I REALLY goinf to have to loop through all the rows and check the column value??? That would be really disgusting.
Answers (6)
0
Administrator

Administrator

Admin 2.3k 1.3m 22y
You can use DataView (sorting is unusually quick), Find() method on it and get DataRow with syntax: DataView[finded-row].Row -or, field- DataView[finded-row].Row[field] DataView[finded-row][field] It's quicker than Select method - it use a pointer to original row but Select() use "sequential scan" and copies the row I think. It's possible to use more than one DataView with own sorts.
0
Administrator

Administrator

Admin 2.3k 1.3m 22y
Doesn't the DataTable.Select() method return an array of DataRow objects? The returned DataRow objects do not have a property that indicates what index/position the row is in the existing DataSet/DataTable, which is the information that I really need. I have a Windows form that allows users to navigate, the old fashioned way, up and down a set of records using databound text boxes and combo boxes. When the user double clicks a name in the TreeView however, I want it to go to the record for the person selected, in the existing databound DataSet. I do not want to create a new DataSet. This is easily done by setting the currency manager to the position of the row in the DataTable.
0
Administrator

Administrator

Admin 2.3k 1.3m 22y
I really don't get why can't you use somethoing like that: myDataTable.Select(LastName, 'Doe'); Mike
0
Administrator

Administrator

Admin 2.3k 1.3m 22y
Thank you all for the suggestions. However, they don't necessarily solve the issue that I was looking at. Essentially, I have a number of fields bound to a dataset. I also have a treeview where one can simply double click on a persons name and it should take you to the record of the person listed, while maintaining the bindings and keeping the entire dataset in tact. What I have done to solve this problem is to sort the view on the same fields that the original dataset is sorted on. I then use the three sorted fields (Last Name, First Name, SSN) with the Find() method on the DataView. Now it returns an index that is identical to the index location in the dataset. Using that index, I simply set the CurrencyManager.Position property to that of the returned index. Whalah!
0
Administrator

Administrator

Admin 2.3k 1.3m 22y
Use DataTable.Select() method instead. Mike
0
Administrator

Administrator

Admin 2.3k 1.3m 22y
Just change the selectcommand in the data adapter and add a where clause. You can adjust this clause based on your criteria. Then fill the DataSet. e.g. "Select.....Where SSN = " + mySocialSecurityNum -Mike