|
-
Dec 13th, 2007, 03:11 AM
#1
Thread Starter
Fanatic Member
Check if Item exist on my datatable
hi guys! help please..I have a Datatable with 3 columns (id,username,password) how will i check if a particular value say, 1 ,does exist in column id of my Datatable? Thanks in advance!
-
Dec 13th, 2007, 03:38 AM
#2
Re: Check if Item exist on my datatable
Assuming ID is the primary key, call the Find method of the Rows collection. If a DataRow object is not returned then the key value doesn't exist.
-
Dec 13th, 2007, 03:58 AM
#3
Thread Starter
Fanatic Member
Re: Check if Item exist on my datatable
Unfurtunately John the datatbale does not have a primary key. hence, ID is not a primary key.I just come around with this solution..
Code:
public bool FuncIsSubjExistInSubjDataTable(string subjID)
{
for (int i = 0; i < dtSubject.Rows.Count; i++)
{
if (dtSubject.Rows[i][0].ToString() == subjID)
return true;
}
return false;
}
Do you have suggestion or comment about this? Thanks in advance.
-
Dec 13th, 2007, 04:05 AM
#4
Re: Check if Item exist on my datatable
You can use a DataView in that case. If the table isn't bound to any controls then use the DefaultView, otherwise create your own DataView:
vb.net Code:
table.DefaultView.Sort = "ID ASC"
If table.DefaultView.Find(id) = -1 Then
'The ID value does not exist.
Else
'At least one row exists with that ID value.
End If
Sorry, wrote that code before I realised I was in C# land. You get the idea though. You can sort a DataView on a column or columns and then get the index of the first row that has specific values in those columns.
-
Dec 13th, 2007, 04:18 AM
#5
Thread Starter
Fanatic Member
Re: Check if Item exist on my datatable
yah no prob...Im somehow familiar with VB..Thanks for that john..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|