Find Position within a DataSet

  • Thread starter Bruce A. Julseth
  • Start date
B

Bruce A. Julseth

I have bound form and am using a CurrencyManager to control navigation on
the form. I want my initial display to be the data for a particular
PrimaryKey. This key changes each time I bind the data. My problem is How do
I find the position within the dataset which I can pass to the
CurrenyManager? I know the PrimaryKey and can locate the row, but I can't
figure out how to find the "Position" value.

Thank you.
 
M

Miha Markic [MVP C#]

Hi Bruce,

When binding to DataTable you actually bind to its DataTable.DefaultView.
Use DefaultView.Find method to find Row's index using primary key.
 
B

Bruce A. Julseth

Miha Markic said:
Hi Bruce,

When binding to DataTable you actually bind to its DataTable.DefaultView.
Use DefaultView.Find method to find Row's index using primary key.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Bruce A. Julseth said:
I have bound form and am using a CurrencyManager to control navigation on
the form. I want my initial display to be the data for a particular
PrimaryKey. This key changes each time I bind the data. My problem is How
do I find the position within the dataset which I can pass to the
CurrenyManager? I know the PrimaryKey and can locate the row, but I can't
figure out how to find the "Position" value.

Thank you.

I'm not sure how to use the Find method in my case. The data in my "Table",
and as I display it, is sorted by date, but I want to "Find" by PrimaryKey.
Also, there are multiple occurrences of any particular date, i.e.. Date is
NOT a unique field?

Can you give me a sample code snip of how I should do this?

Thanks...

Bruce
 
C

Cor Ligthert

Bruce,

What is the problem when you bind to datatable.defaultview?

Set the sort of that to the primarykey and then use the defaultview.find to
find the row index and set the position to that?

Cor
 
M

Miha Markic [MVP C#]

Bruce A. Julseth said:
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
I'm not sure how to use the Find method in my case. The data in my
"Table", and as I display it, is sorted by date, but I want to "Find" by
PrimaryKey.

Find uses primary key value to locate the row.
Also, there are multiple occurrences of any particular date, i.e.. Date is
NOT a unique field?

Then this field can not be pk.
Can you give me a sample code snip of how I should do this?
int rowIndex = table.DefaultView.Find(primaryKeyValue);
 
M

Miha Markic [MVP C#]

Ooops, disregard my previous e-mails.
DataRowCollection.Find (table.Rows) method uses primary key and not
DataView.Find.
So, you should do a nice foreach on the DataView rows instead.
int i = 0;
foreach (DataRowView rowView in table.DefaultView)
{
DataRow row = rowView.Row;
if (row["PrimaryKeyCol"] == yourPK)
{
// i is the index of your row
}
i++
}

-
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Bruce A. Julseth said:
Miha Markic said:
Hi Bruce,

When binding to DataTable you actually bind to its DataTable.DefaultView.
Use DefaultView.Find method to find Row's index using primary key.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Bruce A. Julseth said:
I have bound form and am using a CurrencyManager to control navigation on
the form. I want my initial display to be the data for a particular
PrimaryKey. This key changes each time I bind the data. My problem is How
do I find the position within the dataset which I can pass to the
CurrenyManager? I know the PrimaryKey and can locate the row, but I can't
figure out how to find the "Position" value.

Thank you.

I'm not sure how to use the Find method in my case. The data in my
"Table", and as I display it, is sorted by date, but I want to "Find" by
PrimaryKey. Also, there are multiple occurrences of any particular date,
i.e.. Date is NOT a unique field?

Can you give me a sample code snip of how I should do this?

Thanks...

Bruce
 
C

Cor Ligthert

Bruce,

I made a sample because you and dana have the same problem.

'\\needs a datagrid, a textbox and a button
Private Sub Form9_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Id")
dt.Columns.Add("Name")
dt.LoadDataRow(New Object() {"1", "Dana"}, True)
dt.LoadDataRow(New Object() {"2", "Bruce"}, True)
dt.LoadDataRow(New Object() {"3", "Cor"}, True)
dt.DefaultView.Sort = "Id"
dt.DefaultView.RowFilter = "Id < 3"
DataGrid1.DataSource = dt.DefaultView
DataGrid1.AllowSorting = False
TextBox1.Text = ""
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable = DirectCast(DataGrid1.DataSource,
DataView).Table
Dim dv As New DataView(dt)
dv.Sort = "Id"
Dim pos As Integer = dv.Find("3")
TextBox1.Text = dt.Rows(pos)("Name").ToString
End Sub
///
I hope this helps,

Cor

Bruce A. Julseth said:
Miha Markic said:
Hi Bruce,

When binding to DataTable you actually bind to its DataTable.DefaultView.
Use DefaultView.Find method to find Row's index using primary key.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Bruce A. Julseth said:
I have bound form and am using a CurrencyManager to control navigation on
the form. I want my initial display to be the data for a particular
PrimaryKey. This key changes each time I bind the data. My problem is How
do I find the position within the dataset which I can pass to the
CurrenyManager? I know the PrimaryKey and can locate the row, but I can't
figure out how to find the "Position" value.

Thank you.

I'm not sure how to use the Find method in my case. The data in my
"Table", and as I display it, is sorted by date, but I want to "Find" by
PrimaryKey. Also, there are multiple occurrences of any particular date,
i.e.. Date is NOT a unique field?

Can you give me a sample code snip of how I should do this?

Thanks...

Bruce
 
B

Bruce A. Julseth

Miha Markic said:
Ooops, disregard my previous e-mails.
DataRowCollection.Find (table.Rows) method uses primary key and not
DataView.Find.
So, you should do a nice foreach on the DataView rows instead.
int i = 0;
foreach (DataRowView rowView in table.DefaultView)
{
DataRow row = rowView.Row;
if (row["PrimaryKeyCol"] == yourPK)
{
// i is the index of your row
}
i++
}

-
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Bruce A. Julseth said:
Miha Markic said:
Hi Bruce,

When binding to DataTable you actually bind to its
DataTable.DefaultView.
Use DefaultView.Find method to find Row's index using primary key.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

I have bound form and am using a CurrencyManager to control navigation
on the form. I want my initial display to be the data for a particular
PrimaryKey. This key changes each time I bind the data. My problem is
How do I find the position within the dataset which I can pass to the
CurrenyManager? I know the PrimaryKey and can locate the row, but I
can't figure out how to find the "Position" value.

Thank you.

I'm not sure how to use the Find method in my case. The data in my
"Table", and as I display it, is sorted by date, but I want to "Find" by
PrimaryKey. Also, there are multiple occurrences of any particular date,
i.e.. Date is NOT a unique field?

Can you give me a sample code snip of how I should do this?

Thanks...

Bruce

Miha:

Your code snippet worked. I'm just glad I don't have a couple of million
records. It might be a performance hit.

Does someone have a less cycle eater method of seaching for the primary key
in a Date sorted DataSet?

Thanks...

Bruce
 
B

Bruce A. Julseth

Cor Ligthert said:
Bruce,

I made a sample because you and dana have the same problem.

'\\needs a datagrid, a textbox and a button
Private Sub Form9_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Id")
dt.Columns.Add("Name")
dt.LoadDataRow(New Object() {"1", "Dana"}, True)
dt.LoadDataRow(New Object() {"2", "Bruce"}, True)
dt.LoadDataRow(New Object() {"3", "Cor"}, True)
dt.DefaultView.Sort = "Id"
dt.DefaultView.RowFilter = "Id < 3"
DataGrid1.DataSource = dt.DefaultView
DataGrid1.AllowSorting = False
TextBox1.Text = ""
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable = DirectCast(DataGrid1.DataSource,
DataView).Table
Dim dv As New DataView(dt)
dv.Sort = "Id"
Dim pos As Integer = dv.Find("3")
TextBox1.Text = dt.Rows(pos)("Name").ToString
End Sub
///
I hope this helps,

Cor

Bruce A. Julseth said:
Miha Markic said:
Hi Bruce,

When binding to DataTable you actually bind to its
DataTable.DefaultView.
Use DefaultView.Find method to find Row's index using primary key.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

I have bound form and am using a CurrencyManager to control navigation
on the form. I want my initial display to be the data for a particular
PrimaryKey. This key changes each time I bind the data. My problem is
How do I find the position within the dataset which I can pass to the
CurrenyManager? I know the PrimaryKey and can locate the row, but I
can't figure out how to find the "Position" value.

Thank you.

I'm not sure how to use the Find method in my case. The data in my
"Table", and as I display it, is sorted by date, but I want to "Find" by
PrimaryKey. Also, there are multiple occurrences of any particular date,
i.e.. Date is NOT a unique field?

Can you give me a sample code snip of how I should do this?

Thanks...

Bruce

Cor:

Since I'm trying to locate a Primary Key in date sorted Table that is bound,
will this work? If I sort by Primary Key and do a find on it, the postion
will be different then when it's sorted by date. Correct?

Thanks. for the response.

Bruce
 
C

Cor Ligthert

Bruce,

The find works in a combination with the sort.
I have tried to do this in relation to two sorts.

I think that in that way I can find the row as well in the datatable,
however not set the position right, when there is a datagrid used that is
sorted. I was trying this and saw that the datagrid reset the "position"
after that.

In code do I mean
set a string variable to the current Sort
(be aware that with the datagrid that can be weird because it adds
brackets).
Set the sort to that what is needed for the find.
Find the row in the dataset
Set the sort value back.

Because the fact that behind the scene Net can in my opinion not do be more
efficient than as the sample that Miha showed. Would I take that one Miha
has showed.

Just my thought

Cor
 
M

Miha Markic [MVP C#]

If you do many searches, you might create some sort of index, perhaps by
using hashtable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top