Link to home
Start Free TrialLog in
Avatar of Member_2_5230414
Member_2_5230414

asked on

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

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
Avatar of Rouchie
Rouchie
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_5230414
Member_2_5230414

ASKER

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

Works great although not used Linq before- something I will look into thanks