Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB.net Datatable ->  check if column contains value before adding it

Posted on 2014-02-05
4
3,732 Views
Last Modified: 2014-02-05
Hello all,

I have a datable and I want to check if the column contains a value before adding a row...

so...

If scorestable.Columns("HomeTeam") contains value = "Myteam" then

'Do something

else

 scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)

End if

That way I don't get repeat values
0
Comment
Question by:runnerjp2005
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 39835202
Assuming that you mean you need to check the value of each row in that column, how about:
For each dr as DataRow in scorestable.Rows
  If dr.Item("HomeTeam").ToString.Contains("Myteam") Then
       ' value found - do something, e.g.
       Trace.Warn("Value Exists")
    Exit For
  End If
Next

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39835205
using linq:
  Dim count = scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("HomeTeam").ToString() = "Myteam").Count()
if count = 0 then

'add row
scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)

else
'Do something
end if

Open in new window

0
 

Author Comment

by:runnerjp2005
ID: 39835247
i used the below code but its not adding anything to the table

dim DVScores as new dataview(scorestable)

For each dr as DataRow in DVScores
  If dr.Item("HomeTeam").ToString.Contains(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)) Then
       ' value found - do something, e.g.
	   else
	   
   scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)
		
    Exit For
  End If
Next

Open in new window

0
 

Author Closing Comment

by:runnerjp2005
ID: 39835261
Works great although not used Linq before- something I will look into thanks
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question