Solved

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

Posted on 2014-02-05
4
3,326 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Works great although not used Linq before- something I will look into thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now