By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

What's wrong with this Find on a DataView?

P: n/a
Dim dv As DataView = New DataView(FacilitiesDS1.Facilities, "", "ID
ASC", DataViewRowState.CurrentRows)
Dim iPos As Integer = dv.Find(dr.Item("ID"))
Me.BindingContext(FacilitiesDS1, "Facilities").Position = iPos

That is the code.. dr is DataRow. If dr.Item("ID") = 3, the find will
return position 0, which it should have been 1, and if the ID = 2, it
will return 1, which should have been 0.. The DA has a connection string
that sorts it by name, not ID... Is that where I am running into trouble
here??? ID is the primary key...

Aaron
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Your code looks correct.

You may already be familiar with these debugging techniques, but just in
case, I will tell you what I would do:

When you are stepping through the code, you can highlight any section of
code that will return a basic type, and you will see the value displayed when
you lay your mouse cursor over the highlighted text.

Highlight: dr.Item("ID"), and check to make sure you have the expected value.

In the Command Window-Immediate, during the step through:

Type: ? FacilitiesDS1.Facilities.rows(0).item("ID")
and Enter, to check the datatable rows as they exist in the datatable. Then
check the dataview with ? dv.item(0).item("ID"), etc

Use the up arrow to repeat the line, and put in other rows to make sure the
rows are as you expect.

Since you are sorting the datatable by name, the ID fields will not be in
order in the datatable, but when you pass "ID" as the sort key parameter when
constructing the dataview, the dataview will re-sort on that field.
Dataview.Find only works on the Sort field, and Sort must be set in order to
use Find. It finds the first matching item in that field, and returns the
row.

Hope that helps.

www.charlesfarriersoftware.com

"Aaron Smith" wrote:
Dim dv As DataView = New DataView(FacilitiesDS1.Facilities, "", "ID
ASC", DataViewRowState.CurrentRows)
Dim iPos As Integer = dv.Find(dr.Item("ID"))
Me.BindingContext(FacilitiesDS1, "Facilities").Position = iPos

That is the code.. dr is DataRow. If dr.Item("ID") = 3, the find will
return position 0, which it should have been 1, and if the ID = 2, it
will return 1, which should have been 0.. The DA has a connection string
that sorts it by name, not ID... Is that where I am running into trouble
here??? ID is the primary key...

Aaron
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #2

P: n/a
I stepped through the code yesterday and what I discovered is that I
guess it's actually working correctly, just not how I want it to. When
you apply the sort, it doesn't resort the table.. So the find is
actually working correctly and finding the right position based on the
sort, it's just in the same order.

Here is what I was trying to do, and maybe someone can help me here.
I have a lookup on the table so they can use a grid to sort and quickly
find a record in the table. When that lookup closes, I want to find the
record they selected and go to it. Here is the problem. The table is
sorted by name, but name is not a unique value, the ID field is. When
you tell it to sort by ID, it does, but the position it finds when you
find that record, is not the same as the position in the binding
context. I know there has to be an easy way to do this, any suggestions?

Aaron

Charlie wrote:
Your code looks correct.

You may already be familiar with these debugging techniques, but just in
case, I will tell you what I would do:

When you are stepping through the code, you can highlight any section of
code that will return a basic type, and you will see the value displayed when
you lay your mouse cursor over the highlighted text.

Highlight: dr.Item("ID"), and check to make sure you have the expected value.

In the Command Window-Immediate, during the step through:

Type: ? FacilitiesDS1.Facilities.rows(0).item("ID")
and Enter, to check the datatable rows as they exist in the datatable. Then
check the dataview with ? dv.item(0).item("ID"), etc

Use the up arrow to repeat the line, and put in other rows to make sure the
rows are as you expect.

Since you are sorting the datatable by name, the ID fields will not be in
order in the datatable, but when you pass "ID" as the sort key parameter when
constructing the dataview, the dataview will re-sort on that field.
Dataview.Find only works on the Sort field, and Sort must be set in order to
use Find. It finds the first matching item in that field, and returns the
row.

Hope that helps.

www.charlesfarriersoftware.com

--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
Nov 21 '05 #3

P: n/a
I use a derived grid for allowing the user to locate a record, much as you
are describing. There may be a better way to do this, but I found a way to
assure that the Key (or ID) value was properly identified.

I provide a hidden (or unhidden) column in the datagrid for the Key

I allow the user sort the grid by column, so to get the key, I extract it
out of the grid in the mousedown event, using a HitTest, instead of trying to
identify it in the row of the underlying data...

Private Sub DataGrid1_MouseDown(ByVal sender _
As System.Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) _
Handles DataGrid1.MouseDown
Dim Key As String
Dim HTI As DataGrid.HitTestInfo = _
DataGrid1.HitTest(e.X, e.Y)
Select Case DataGrid1.HitTest(e.X, e.Y).Type
Case DataGrid.HitTestType.RowHeader, _
DataGrid.HitTestType.Cell
Key = DataGrid1.Item(HTI.Row, 0)
End Select
End Sub

This is a simplified version of what I have in the derived control, and I
did not test this code, but it should work, or at least be close.

The Column containing the Key may be hidden by using TableStyles for the
DataGrid, and setting the Key column width to 0. In the code above, the Key
is in Column 0 of the grid.

Hope that helps.

www.charlesfarriersoftware.com
"Aaron Smith" wrote:
I stepped through the code yesterday and what I discovered is that I
guess it's actually working correctly, just not how I want it to. When
you apply the sort, it doesn't resort the table.. So the find is
actually working correctly and finding the right position based on the
sort, it's just in the same order.

Here is what I was trying to do, and maybe someone can help me here.
I have a lookup on the table so they can use a grid to sort and quickly
find a record in the table. When that lookup closes, I want to find the
record they selected and go to it. Here is the problem. The table is
sorted by name, but name is not a unique value, the ID field is. When
you tell it to sort by ID, it does, but the position it finds when you
find that record, is not the same as the position in the binding
context. I know there has to be an easy way to do this, any suggestions?

Aaron

Charlie wrote:
Your code looks correct.

You may already be familiar with these debugging techniques, but just in
case, I will tell you what I would do:

When you are stepping through the code, you can highlight any section of
code that will return a basic type, and you will see the value displayed when
you lay your mouse cursor over the highlighted text.

Highlight: dr.Item("ID"), and check to make sure you have the expected value.

In the Command Window-Immediate, during the step through:

Type: ? FacilitiesDS1.Facilities.rows(0).item("ID")
and Enter, to check the datatable rows as they exist in the datatable. Then
check the dataview with ? dv.item(0).item("ID"), etc

Use the up arrow to repeat the line, and put in other rows to make sure the
rows are as you expect.

Since you are sorting the datatable by name, the ID fields will not be in
order in the datatable, but when you pass "ID" as the sort key parameter when
constructing the dataview, the dataview will re-sort on that field.
Dataview.Find only works on the Sort field, and Sort must be set in order to
use Find. It finds the first matching item in that field, and returns the
row.

Hope that helps.

www.charlesfarriersoftware.com

--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #4

P: n/a
Also, you probably already know this, but jic..
To get the DataRow from the DataTable, using the Key:
DR = DT.Rows.Find(Key)
To use rows.find, the Datatable must have its PrimaryKey property defined.

"Aaron Smith" wrote:
I stepped through the code yesterday and what I discovered is that I
guess it's actually working correctly, just not how I want it to. When
you apply the sort, it doesn't resort the table.. So the find is
actually working correctly and finding the right position based on the
sort, it's just in the same order.

Here is what I was trying to do, and maybe someone can help me here.
I have a lookup on the table so they can use a grid to sort and quickly
find a record in the table. When that lookup closes, I want to find the
record they selected and go to it. Here is the problem. The table is
sorted by name, but name is not a unique value, the ID field is. When
you tell it to sort by ID, it does, but the position it finds when you
find that record, is not the same as the position in the binding
context. I know there has to be an easy way to do this, any suggestions?

Aaron

Charlie wrote:
Your code looks correct.

You may already be familiar with these debugging techniques, but just in
case, I will tell you what I would do:

When you are stepping through the code, you can highlight any section of
code that will return a basic type, and you will see the value displayed when
you lay your mouse cursor over the highlighted text.

Highlight: dr.Item("ID"), and check to make sure you have the expected value.

In the Command Window-Immediate, during the step through:

Type: ? FacilitiesDS1.Facilities.rows(0).item("ID")
and Enter, to check the datatable rows as they exist in the datatable. Then
check the dataview with ? dv.item(0).item("ID"), etc

Use the up arrow to repeat the line, and put in other rows to make sure the
rows are as you expect.

Since you are sorting the datatable by name, the ID fields will not be in
order in the datatable, but when you pass "ID" as the sort key parameter when
constructing the dataview, the dataview will re-sort on that field.
Dataview.Find only works on the Sort field, and Sort must be set in order to
use Find. It finds the first matching item in that field, and returns the
row.

Hope that helps.

www.charlesfarriersoftware.com

--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.