DataRow with specified value & index ?
-
In VB.NET 2 part question: I want a DataRow that contains a column with a specified value. Part 1: would the following work to identify the row that contains the value and assign it to the DataRow?: drPart = DsParts1.Tables("EventID").Rows.Find(iEventId) ‘where EventID is the column and iEventId is the value. Part 2: Once I have identified the row with the value and assigned it to drPart, how do I assign the index of that row in the table to iIndex variable? Thanks.
-
In VB.NET 2 part question: I want a DataRow that contains a column with a specified value. Part 1: would the following work to identify the row that contains the value and assign it to the DataRow?: drPart = DsParts1.Tables("EventID").Rows.Find(iEventId) ‘where EventID is the column and iEventId is the value. Part 2: Once I have identified the row with the value and assigned it to drPart, how do I assign the index of that row in the table to iIndex variable? Thanks.
1. Will work only if the value you are searching for is in your PrimaryKey column. If not use the DataTable.Select function. You can even use complex queries there. 2. I think there is no elegant way to get the index. If your really need it you will have to determine it yourself:
public int IndexOf(DataRow row, DataTable table) { for (int i = 0; i < table.Rows.Count; i++) if (table.Rows[i] == row) return i; return -1; }
Note that this is not very effective and should not be used with large DataTables. -
1. Will work only if the value you are searching for is in your PrimaryKey column. If not use the DataTable.Select function. You can even use complex queries there. 2. I think there is no elegant way to get the index. If your really need it you will have to determine it yourself:
public int IndexOf(DataRow row, DataTable table) { for (int i = 0; i < table.Rows.Count; i++) if (table.Rows[i] == row) return i; return -1; }
Note that this is not very effective and should not be used with large DataTables.Thank you, Robert. I have been working on it and between two VB.NET books I was able to figure out how to use select method to form a subset. You are right about using select, it was much simpler than what I had originally thought to do. I was going to use the index (if it existed) and move through the data table to find the rows I wanted.