CurrencyManager IndexOf

J

Jeff

I have a situation where I need to recursively position the CurrencyManager
of a DataRow's parent DataRow.

The problem is that the CurrencyManager.List contains DataRowView objects
that I want to find via IndexOf, but I when I GetParentRows... I get a
"DataRow". Any ideas on how I can get a DataRowView instead?

DataSet is Northwind... Customers, Orders, OrderDetails via 2 DataRelations.

The struct below just holds a DataRow and it's child relation navigation
path.

Try following the code below as if the UI caused the OrderDetails
CurrencyManager to change positions.

private void cm_PositionChanged(object sender, EventArgs e)

{

CurrencyManager cm = (CurrencyManager)sender;

String navPath;


if (cm.Position >= 0)

{

DataRowView drv = (DataRowView)((DataView)cm.List)[cm.Position];

DataRow dr = drv.Row;

if (dr.RowState != DataRowState.Detached)

{

ArrayList dataRows = new ArrayList();

dataRows.Add(new RowRelation("", dr)); // this is the DataRow
actually selected

while (dr.Table.ParentRelations.Count > 0)

{

navPath = dr.Table.ParentRelations[0].RelationName;

dr = dr.GetParentRow(dr.Table.ParentRelations[0]); //
this is the relative parent DataRow, what I really need the DataRowView of
the Parent

dataRows.Add(new RowRelation(navPath, dr));

}

navPath =
((RowRelation)dataRows[dataRows.Count-1]).DataRow.Table.TableName;

RowRelation rr;

for (int i = dataRows.Count; i>0; i--)

{

rr = (RowRelation)dataRows[i-1];

dr = rr.DataRow;

cm = (CurrencyManager)BindingContext[dr.Table.DataSet,
navPath];

navPath = navPath + "." + rr.NavigationPath;

// **** Here is where I need the DataRowView ****

//Console.WriteLine(cm.List.IndexOf(dr).ToString()); //
Always returns -1 becuase cm.List contains DataRowView's not DataRow's eh?

}

}

}


}

private struct RowRelation

{

public String NavigationPath;

public DataRow DataRow;

public RowRelation(String navigationPath, DataRow dataRow)

{

NavigationPath = navigationPath;

DataRow = dataRow;

}

}
 
M

Miha Markic [MVP C#]

Hi Jeff,

You can't because view has no knowledge of relations nor it cares which view
is used to represent other tables.
You'll have to use pk of parent row to find the parent's DataRowView.
 
J

Jeff

Thanks for the feedback. I guess I should've deduced the same. Anyway I've
modified it slightly and now do as you say, find the records via their PK.

Thank you again.


Miha Markic said:
Hi Jeff,

You can't because view has no knowledge of relations nor it cares which
view is used to represent other tables.
You'll have to use pk of parent row to find the parent's DataRowView.

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

Jeff said:
I have a situation where I need to recursively position the
CurrencyManager of a DataRow's parent DataRow.

The problem is that the CurrencyManager.List contains DataRowView objects
that I want to find via IndexOf, but I when I GetParentRows... I get a
"DataRow". Any ideas on how I can get a DataRowView instead?

DataSet is Northwind... Customers, Orders, OrderDetails via 2
DataRelations.

The struct below just holds a DataRow and it's child relation navigation
path.

Try following the code below as if the UI caused the OrderDetails
CurrencyManager to change positions.

private void cm_PositionChanged(object sender, EventArgs e)

{

CurrencyManager cm = (CurrencyManager)sender;

String navPath;


if (cm.Position >= 0)

{

DataRowView drv = (DataRowView)((DataView)cm.List)[cm.Position];

DataRow dr = drv.Row;

if (dr.RowState != DataRowState.Detached)

{

ArrayList dataRows = new ArrayList();

dataRows.Add(new RowRelation("", dr)); // this is the
DataRow actually selected

while (dr.Table.ParentRelations.Count > 0)

{

navPath = dr.Table.ParentRelations[0].RelationName;

dr = dr.GetParentRow(dr.Table.ParentRelations[0]); //
this is the relative parent DataRow, what I really need the DataRowView
of the Parent

dataRows.Add(new RowRelation(navPath, dr));

}

navPath =
((RowRelation)dataRows[dataRows.Count-1]).DataRow.Table.TableName;

RowRelation rr;

for (int i = dataRows.Count; i>0; i--)

{

rr = (RowRelation)dataRows[i-1];

dr = rr.DataRow;

cm = (CurrencyManager)BindingContext[dr.Table.DataSet,
navPath];

navPath = navPath + "." + rr.NavigationPath;

// **** Here is where I need the DataRowView ****

//Console.WriteLine(cm.List.IndexOf(dr).ToString()); //
Always returns -1 becuase cm.List contains DataRowView's not DataRow's
eh?

}

}

}


}

private struct RowRelation

{

public String NavigationPath;

public DataRow DataRow;

public RowRelation(String navigationPath, DataRow dataRow)

{

NavigationPath = navigationPath;

DataRow = dataRow;

}

}
 

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

Similar Threads


Top