Simple way of finding whether a certain value is in a DataSet column

N

nvx

Hi,
is there a simple and elegant way of finding whether a given string
value is already present in DataSet column? Although I may do this
using a For statement or something similar, I'd like to avoid using
cycles. I'm looking for a method of DataSet, DataSet.Table etc., if
there is any. Unfortunately, I was not able to find anything like
that.

Second question:
I also need to find the row to which a given row of a DataGridView is
bound. Again, I would prefer a simple method-based solution, if it
exists... :)

Thanks very much for any help...

With regards
nvx
 
W

William \(Bill\) Vaughn

It sounds like you're asking for the features of a SQL query engine. That's
not supported in ADO.NET (at least not yet). There is nothing to stop you
from walking the DataSet Table(n).Rows(n).Item(n) and testing...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
J

Jim Rand

Look at DataViews.

private void button1_Click(object sender, EventArgs e)
{
DataView view = new DataView(dsNorthwind.Customer, "Country = 'USA'",
null, DataViewRowState.CurrentRows);
if (view.Count > 0) MessageBox.Show("USA exists");

foreach (DataRowView rowView in view)
{
DSNorthwind.CustomerRow customerRow = rowView.Row as
DSNorthwind.CustomerRow;
MessageBox.Show(customerRow.CompanyName, customerRow.Country);
}
}
 
W

William \(Bill\) Vaughn

I like that a lot better...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
 
N

nvx

Thanks a lot, both of you... I'll see what I can do with the things
you've suggested...

With regards
nvx


Jim Rand napsal:
 
N

nvx

After a few minutes of coding I may say this DataView based algorithm
works like a charm... Thanks again... :)

With regards
nvx


Jim Rand napsal:
 
M

Miha Markic [MVP C#]

Yep, and there is also DataTable.Select method that does similar wizardry -
instead of having an instance of DataView you get an array of rows.
 
C

Cor Ligthert [MVP]

And even easier, the DataView Find, which gives back the index of the row.

http://msdn2.microsoft.com/en-gb/library/46d41xk2.aspx

Or if you want a row instead of an index, DataRowCollection.find

http://msdn2.microsoft.com/en-us/library/ydd48eyk.aspx


Now we have again all 4 ADONET alternatives in one thread,

:)

Cor

Miha Markic said:
Yep, and there is also DataTable.Select method that does similar
wizardry - instead of having an instance of DataView you get an array of
rows.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Jim Rand said:
Look at DataViews.

private void button1_Click(object sender, EventArgs e)
{
DataView view = new DataView(dsNorthwind.Customer, "Country = 'USA'",
null, DataViewRowState.CurrentRows);
if (view.Count > 0) MessageBox.Show("USA exists");

foreach (DataRowView rowView in view)
{
DSNorthwind.CustomerRow customerRow = rowView.Row as
DSNorthwind.CustomerRow;
MessageBox.Show(customerRow.CompanyName, customerRow.Country);
}
}
 
N

nvx

Many thanks, guys... I just hope I won't forget all this additional
info untill I need it. ;)

nvx


Cor Ligthert [MVP] napsal:
And even easier, the DataView Find, which gives back the index of the row.

http://msdn2.microsoft.com/en-gb/library/46d41xk2.aspx

Or if you want a row instead of an index, DataRowCollection.find

http://msdn2.microsoft.com/en-us/library/ydd48eyk.aspx


Now we have again all 4 ADONET alternatives in one thread,

:)

Cor

Miha Markic said:
Yep, and there is also DataTable.Select method that does similar
wizardry - instead of having an instance of DataView you get an array of
rows.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Jim Rand said:
Look at DataViews.

private void button1_Click(object sender, EventArgs e)
{
DataView view = new DataView(dsNorthwind.Customer, "Country = 'USA'",
null, DataViewRowState.CurrentRows);
if (view.Count > 0) MessageBox.Show("USA exists");

foreach (DataRowView rowView in view)
{
DSNorthwind.CustomerRow customerRow = rowView.Row as
DSNorthwind.CustomerRow;
MessageBox.Show(customerRow.CompanyName, customerRow.Country);
}
}


Hi,
is there a simple and elegant way of finding whether a given string
value is already present in DataSet column? Although I may do this
using a For statement or something similar, I'd like to avoid using
cycles. I'm looking for a method of DataSet, DataSet.Table etc., if
there is any. Unfortunately, I was not able to find anything like
that.

Second question:
I also need to find the row to which a given row of a DataGridView is
bound. Again, I would prefer a simple method-based solution, if it
exists... :)

Thanks very much for any help...

With regards
nvx
 

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