DataTable Find/Update

G

Guest

Hello,

I have a basic question. I have a DataTable of information without a
database store (it's only in memory). I am looking to somehow query the
DataTable to find out which row(s) satisfy certain criteria and then update
the data in those rows, all without any sort of database access (no
DataAdapters or anything). The table does not have a primary key so I've
been using DataTable.DefaultView.Find to get the index of the row that
satisfies the criteria. This is also supposed to be faster than Select() or
other methods that require re-indexing the table, which I would like to avoid
since I have to do this for multiple queries. But once get the index, how do
I use it to access the row that was just found to update the data in it?
I've attempted

myDataTable.DefaultView.Sort = SortColumns;
int FoundRow = myDataTable.DefaultView.Find(FindValues);

if(FoundRow != -1)
myDataTable.Rows[FoundRow]["Name"] = "Joe";

and

myDataTable.DefaultView.Sort = SortColumns;
int FoundRow = myDataTable.DefaultView.Find(FindValues);

if(FoundRow != -1)
myDataTable.DefaultView.Table.Rows[FoundRow]["Name"] = "Joe";

where SortColumns is the string of columns (i.e. "FirstName, LastName") and
FindValues is the object array of values to search for (i.e. {"Joey","Jimbo"})
and neither work (the row that "Joe" is saved into does not match the
FindValues on the SortColumns fields, so it's obviously not the row that
Find() is finding). How do I get to the row that Find is finding? Thanks
for your help!
 
B

Brock Allen

DataView.Select returns an array of DataRowView objects. Why can't you use
those?

DataRowView[] rows = dv.Find(...);
rows[0]["SomeColumnName"] = "NewValue";
 
G

Guest

Brock,

Thanks for your post. The reason I am not looking to use DataView.Select or
DataTable.Rows.Find is because they require a primary key to be added to the
table. The table does not have a primary key and I am not looking to add one
programmatically since the table will be large and it could be a problem with
speed. I have read that for speed, using DefaultView.Find is best. I am
just attempting to figure out how the integer returned corresponds to rows in
the table (i.e. how to access the row in the table using the returned
integer). Thanks for your help!

Brock Allen said:
DataView.Select returns an array of DataRowView objects. Why can't you use
those?

DataRowView[] rows = dv.Find(...);
rows[0]["SomeColumnName"] = "NewValue";




Hello,

I have a basic question. I have a DataTable of information without a
database store (it's only in memory). I am looking to somehow query
the DataTable to find out which row(s) satisfy certain criteria and
then update the data in those rows, all without any sort of database
access (no DataAdapters or anything). The table does not have a
primary key so I've been using DataTable.DefaultView.Find to get the
index of the row that satisfies the criteria. This is also supposed
to be faster than Select() or other methods that require re-indexing
the table, which I would like to avoid since I have to do this for
multiple queries. But once get the index, how do I use it to access
the row that was just found to update the data in it? I've attempted

myDataTable.DefaultView.Sort = SortColumns;
int FoundRow = myDataTable.DefaultView.Find(FindValues);
if(FoundRow != -1)
myDataTable.Rows[FoundRow]["Name"] = "Joe";
and

myDataTable.DefaultView.Sort = SortColumns;
int FoundRow = myDataTable.DefaultView.Find(FindValues);
if(FoundRow != -1)
myDataTable.DefaultView.Table.Rows[FoundRow]["Name"] = "Joe";
where SortColumns is the string of columns (i.e. "FirstName,
LastName") and FindValues is the object array of values to search for
(i.e. {"Joey","Jimbo"}) and neither work (the row that "Joe" is saved
into does not match the FindValues on the SortColumns fields, so it's
obviously not the row that Find() is finding). How do I get to the
row that Find is finding? Thanks for your help!

Mark Fox
 
G

Guest

I've figured this out. It's

myDataTable.DefaultView[FoundRow]["Name"] = "Joe";

instead of

myDataTable.DefaultView.Table.Rows[FoundRow]["Name"] = "Joe";


Solel Software said:
Brock,

Thanks for your post. The reason I am not looking to use DataView.Select or
DataTable.Rows.Find is because they require a primary key to be added to the
table. The table does not have a primary key and I am not looking to add one
programmatically since the table will be large and it could be a problem with
speed. I have read that for speed, using DefaultView.Find is best. I am
just attempting to figure out how the integer returned corresponds to rows in
the table (i.e. how to access the row in the table using the returned
integer). Thanks for your help!

Brock Allen said:
DataView.Select returns an array of DataRowView objects. Why can't you use
those?

DataRowView[] rows = dv.Find(...);
rows[0]["SomeColumnName"] = "NewValue";




Hello,

I have a basic question. I have a DataTable of information without a
database store (it's only in memory). I am looking to somehow query
the DataTable to find out which row(s) satisfy certain criteria and
then update the data in those rows, all without any sort of database
access (no DataAdapters or anything). The table does not have a
primary key so I've been using DataTable.DefaultView.Find to get the
index of the row that satisfies the criteria. This is also supposed
to be faster than Select() or other methods that require re-indexing
the table, which I would like to avoid since I have to do this for
multiple queries. But once get the index, how do I use it to access
the row that was just found to update the data in it? I've attempted

myDataTable.DefaultView.Sort = SortColumns;
int FoundRow = myDataTable.DefaultView.Find(FindValues);
if(FoundRow != -1)
myDataTable.Rows[FoundRow]["Name"] = "Joe";
and

myDataTable.DefaultView.Sort = SortColumns;
int FoundRow = myDataTable.DefaultView.Find(FindValues);
if(FoundRow != -1)
myDataTable.DefaultView.Table.Rows[FoundRow]["Name"] = "Joe";
where SortColumns is the string of columns (i.e. "FirstName,
LastName") and FindValues is the object array of values to search for
(i.e. {"Joey","Jimbo"}) and neither work (the row that "Joe" is saved
into does not match the FindValues on the SortColumns fields, so it's
obviously not the row that Find() is finding). How do I get to the
row that Find is finding? Thanks for your help!

Mark Fox
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top