Click here to Skip to main content
11,803,561 members (67,321 online)
Rate this: bad
Please Sign up or sign in to vote.
I've created a Dictionary like

Dictionary<department,bool> dict= new Dictionary<department,bool>();

here Department is a class and I have `DepartmentId,Name and Code` for the departments. And in the `bool` am sending whether the person is `HOD` or not.

Am adding the records to this Dictionary like


here the records are successfully added to the Dictionary and after this am binding the Dictionary to a GridView like


now the inserted records are displayed fine in the gridview. After this am storing this records in the 'StaffDepartments' table in my database. I have 3 columns in the 'Staffdepartments' table

1.StaffId(PK - has link with the Staff table)
2.DepartmentId(PK - has link with the Department table)

here the records are stored fine in the database.No problem in adding the records into the database.

I have some questions here

****1.How can check whether the DepartmentId is already there in the Dictionary before adding to it.
2.When am editing the staff detail how can I delete the Selected Department from the Dictionary by checking the checkbox in Gridview rows.(here the records are coming from the database, so when I click delete button the records should be deleted in the database as well)****

if its a List instead of Dictionary, I can get the DepartmentId by

int departmentId = (int)gridDept.DataKeys[row.RowIndex].Values["DepartmentId"];

but in Dictionary i dunno how to do the same with Key and Value pairs....can anyone help me here.
Posted 17-Apr-12 1:28am
Edited 17-Apr-12 1:34am
abdul samathu at 17-Apr-12 7:49am
thanks Uday
Mohamed Mitwalli at 17-Apr-12 9:08am
Hi ,
1.How can check whether the DepartmentId is already there in the Dictionary before adding to it.
for This one i believe you have loop through Dictionary and compare the key and value with the one your trying to add .
2. For second one , It has to be some unique value to compare it with and if it true you removed from Dictionary and Database .
abdul samathu at 17-Apr-12 10:49am
thanks Mohamed Mitwalli, I dunno how to do this

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


What you will need to do is to query your dictionary using LINQ

List<dict> mylist = new List<dict>
mylist = dict.where(x=>x.value.deptID=yourdepID).tolist();
// value exists
// to update you will need to create a new entry
// delete the old one
// insert the new value
//insert new

The syntax may not be 100% correct as I cannot test at the moment.

Hope this helps you.
abdul samathu at 18-Apr-12 0:36am
if I tried to create a List with dict, as u suggested reports error like "dict is a field, but is used like a field"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 CPallini 290
2 F-ES Sitecore 270
3 Sergey Alexandrovich Kryukov 234
4 CHill60 220
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 17 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100