Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
5.00/5 (1 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)<br />
 2.DepartmentId(PK - has link with the Department table)<br />

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.
Updated 17-Apr-12 1:34am
abdul samathu 17-Apr-12 7:49am
thanks Uday
Mohamed Mitwalli 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 17-Apr-12 10:49am
thanks Mohamed Mitwalli, I dunno how to do this

1 solution


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 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)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900