469,958 Members | 1,944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,958 developers. It's quick & easy.

DataTable Find/Update

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!

--
Sincerely,

Mark Fox
Nov 19 '05 #1
3 6184
DataView.Select returns an array of DataRowView objects. Why can't you use
those?

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

-Brock
DevelopMentor
http://staff.develop.com/ballen
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


Nov 19 '05 #2
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" wrote:
DataView.Select returns an array of DataRowView objects. Why can't you use
those?

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

-Brock
DevelopMentor
http://staff.develop.com/ballen
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


Nov 19 '05 #3
I've figured this out. It's

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

instead of

myDataTable.DefaultView.Table.Rows[FoundRow]["Name"] = "Joe";
"Solel Software" wrote:
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" wrote:
DataView.Select returns an array of DataRowView objects. Why can't you use
those?

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

-Brock
DevelopMentor
http://staff.develop.com/ballen
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


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris | last post: by
2 posts views Thread by Alvin Bruney | last post: by
4 posts views Thread by George | last post: by
2 posts views Thread by Fred W. | last post: by
1 post views Thread by Tim Kelley | last post: by
4 posts views Thread by Arne Beruldsen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.