Newbie: Finding the position of a DataRow in a DataGrid that's sorted.

T

Tyson Marchuk

New to C# and databases...

I've been searching newsgroups and websites for a while and can't find an
answer so I figured I'd ask... :)

What I want to do is determine the row number in a DataGrid that coresponds
to a DataRow in the associated DataTable. (I also know the row number of
this DataRow in the DataTable if that helps.)

I want to do this because I'm programatically adding/updating items to this
DataTable when the user does various other things in the application and I
want to select/highlight the row that was changed/added and make it the
currently selected row. I can't use the row number in the DataTable because
if the user sorts the DataGrid these numbers no longer match.

Note: I'm not looking to find the DataRow for the currently selected row.
There's lots of code showing how to do this using BindingManagerBase... I'm
trying to set the current row based on a DataRow that I've modified.

Of course there's the brute-force approach of going though all rows in the
DataGrid looking for my Primary Key value but surely there's a more
efficient way of doing this?

Thanks for any help you can provide,
-Tyson Marchuk
 
C

Cor Ligthert

Tyson,

Because of the different situations it is almost impossible to give you a
sample. However the seldom seen method in this newsgroups or on Internet is
probably the way to go for you.

This gives you a position on the dataview. When that dataview is the
datasource for your datagrid, than it would in my idea be the row that you
want to select.

In my opinion is there not a direct 1=1 answer therefore I have seen this
question to often. However you never know.

http://msdn.microsoft.com/library/d...ml/frlrfsystemdatadataviewclassfindtopic2.asp

I hope this helps?

Cor
 
T

Tyson Marchuk

I'm not sure this helps... I've tried using a DataView before and the main
problem is that the 'find' methods only search the column that is currently
sorted.

So.. say we have the following table as an example:

Quantity ItemID ItemName
3 001 "Coffee - Black"
1 015 "Donut - Boston Creme"
1 005 "Donut - Plain"

Now if the user sorts this list by Quantity then we'll have the 2 donuts
listed before the coffees (I'm not sure what order given the duplicates but
it doesn't really matter). Now if I want to add for example a muffin and
highlight that column I can add the row to the DataTable easily but how do I
know which row it'll be in the DataGrid?

If I do a DataView::find() call on the associated DataView it'll be
searching the Quantity column... but there are now 3 items in the quantity
column that have a value of '1'. So which one did I get? If I make my own
DataView and sort it by the 'ItemID' PrimaryKey then I always get the right
row but then the row number doesn't match the one the user has in his sorted
DataGrid... sigh.

The more I thought about this problem overnight the more I'm not sure there
can be a nice 1=>1 answer going the direction I want because then a DataRow
or a DataRowView would need to know about it's row number and this is
probably something a DataView doesn't want because it's moving these around
a lot. So I guess it just comes down to brute-force searching. Still it
would be nice if the DataGrid or DataView classes had a 'find()' function
that would do this for you on columns other than the sorted one.

I guess I'll just write a loop that goes through all the DataGrid rows
looking for my PrimaryKey. I thought there was a nicer way to do this but
it shouldn't be too bad performance-wise because I'll never have more than
about 20 items in this list but I was hoping for a more elegant solution.

Thanks for your time Cor,
-Tyson
 
C

Cor Ligthert

Tyson,

I tried it, and I don't see your problem, maybe you can try this and explain
it than to me more.
It only needs a new project a datagrid and a button on a form and than
setting the two events and paste this in.
\\\
private void button1_Click(object sender, System.EventArgs e)
{
DataView dv = (DataView)this.dataGrid1.DataSource;
DataTable dt = dv.Table;
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {2,"002","Thee - Ceylon"};
dt.Rows.Add(dr);
int i = 0;
if ((string)dv.Sort == "[ItemId]") {i = dv.Find("002");}
else if (dv.Sort == "[Quantity]") {i = dv.Find(2);}
else if (dv.Sort == "[ItemName]") {i = dv.Find("Thee - Ceylon");}
this.dataGrid1.Select(i);
}
private void FormLoad(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Quantity");
dt.Columns.Add("ItemID");
dt.Columns.Add("ItemName");
for (int i = 0; i < 3; i++) {
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);}
dt.Rows[0].ItemArray = new object[] {3,"001","Coffee - Black"};
dt.Rows[1].ItemArray = new object[] {1,"015", "Donut - Boston Creme"};
dt.Rows[2].ItemArray = new object[] {1,"005","Donut - Plain"};
DataView dv = new DataView(dt);
this.dataGrid1.DataSource = dv;
}
}
///

I hope this helps?

Cor
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Tyson,
What I want to do is determine the row number in a DataGrid that coresponds
to a DataRow in the associated DataTable. (I also know the row number of
this DataRow in the DataTable if that helps.)

The DataGrid's row should correspond with DataView index.
If you don't use any DataView then you can try to check
DataTable.DefaulView as a DataView.
I want to do this because I'm programatically adding/updating items to this
DataTable when the user does various other things in the application and I
want to select/highlight the row that was changed/added and make it the
currently selected row. I can't use the row number in the DataTable because
if the user sorts the DataGrid these numbers no longer match.

Note: I'm not looking to find the DataRow for the currently selected row.
There's lots of code showing how to do this using BindingManagerBase... I'm
trying to set the current row based on a DataRow that I've modified.

// there is solution that should work
DataRow selectedRow=dataView[dataGrid.CurrentRowIndex].Row;
Of course there's the brute-force approach of going though all rows in the
DataGrid looking for my Primary Key value but surely there's a more
efficient way of doing this?

I can recommend to use your own DataView object to check
any User-IO on DataGrid.

HTH
Marcin
 

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