searching for entries

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
Is it possible to search either a datarow or datatable for a value?

I see the the dataview has a .find method but cannot see anything in the datarow or datatable?

I thought I could do something like...

drDataRow.Find("can I find this?")

or

dtDataTable.Find("can I find this?")

Or maybe .seek?

Thanks
 
DataTables have a property called DefaultView which is of type dataview.
So you can use
VB.NET:
drDataTable.DefaultView.Find("yes you can find this!")
 
Hi, I had found this:

VB.NET:
If glb_dtTransactionDesc.Rows.Find(txtTextBox.Text) Is Nothing Then
that errors that the table in question does not have a primary key?

Now the table is filled by the use of .fill against a table which does have a primary key?

I found a property glb_dtTransactionDesc.PrimaryKey which I am trying to set this I presume to the primary key column in the datatable but cant figure it out!

The datatable only has one column which is the primary one.

Sorry forgot to say I tried your way but I get an errror saying the result is an int?
 
Sorry forgot to say I tried your way but I get an errror saying the result is an int?
If you look at the definition of the DataView.Find method in either MSDN or the object browser you will find that it returns: "The index of the row in the DataView that contains the sort key value specified; otherwise -1 if the sort key value does not exist."
Note that in Visual Studio you can place the cursor in a class's method then press F2 to open the object broswer to the definition of that class. Pressing F1 will open help to the definition.
 
Hi, thanks.

I got the primary key bit sorted which means the method above works a treat :)

VB.NET:
' set the primary key for this table 

glb_dtTransactionDesc.PrimaryKey = New DataColumn() {glb_dtTransactionDesc.Columns("tran_description")}
 
To search a DataTable for something, use the SELECT command and pass in something that looks like an SQL where clause:

Dim foundRows() as DataRow 'array

foundRows = myDataTable.Select("[first_name] LIKE 'cjar*'")


the array now contains all rows whose [first_name] column starts with cjar

You cannot searcha datarow.. its too small to bother with! Just do a for loop to iterate the row items() collection
 
Hi, I had forgotten about this post and came across it whilst searching for the solution to my current problem.

It helps slightly, but not completely.

Firstly I could not get the GetUpperBounds method of the rwFindRows array to work? I had to use the Length method to tell me how many rows I got returned?

Secondly, no matter how much I try I cannot access the data held in the rwFindRows array? How do I access the columns of each row held in the row array?

Thanks

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rwFindRows() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
[/SIZE][SIZE=2][COLOR=#008000]' get all rows that have a transaction id of 81
[/COLOR][/SIZE][SIZE=2]rwFindRows = dtTable.Select([/SIZE][SIZE=2][COLOR=#800000]"[tran_id] = 81"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]' update the form label with remaining payments due
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblPaymentsRemaining.Text = (36 - [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](rwFindRows.Length.ToString)).ToString
[/SIZE]
 
Your array rwFindRows runs from 0 to rwFindRows.Length-1

For a array with 10 rows, this gets row 7:
rwFindRows(6)

As for accessing the cells, dont forget that a row is itself a collection (like an array), so an array of rows is like a 2D array:


'find the third cell of the seventh row- again, cells are numbered from 0
rwFindRows(6)(2)

'find the cell whose column name is fred:
rwFindROws(6)("fred")


Find the number of columns, either of:
rwFIndRows(6).ItemArray.Length
rwFindRows(6).Table.Columns.Count
 
Specify the appropriate index of the array to get a reference to a DataRow then use the Item property:
VB.NET:
rwFindRows(1).Item("columnName")
The '1' in the example can be any valid index in the array. I have shown the overloaded Item property using a string as the parameter but there are 6 possible overloads to choose from.
 
Paszt.. a DataRow has no Item property?! ;)

(arg.. someone's turned off the auto-show-image-attachments as images option.. click the attachments to see the pics)
???


myDataRow.Item(2)


should be written as:

myDataRow(2)


and can be written as this in very limited situations:

myDataRow.ItemArray(2)


Always prefer DataRow(index) to the former, because it has useful overloads, whereas the latter is merely an array indexer :D
 

Attachments

  • Image3.gif
    Image3.gif
    41.8 KB · Views: 21
  • Image4.gif
    Image4.gif
    8.7 KB · Views: 18
Last edited:
Back
Top