与GridView的字典 [英] Gridview with Dictionary

查看:94
本文介绍了与GridView的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个字典像

 词典<部,布尔>字典=新词典<部,布尔>();

下面部的一类,我有 ID,名称和code 的部门。而在布尔我发送人是否 HOD 或没有。

我添加的记录本字典一样

  dict.Add(部门,chkHOD.checked);

在这里记录被成功添加到词典并在此之后我绑定的字典到GridView像

  gridDept.Datasource =字典;
gridDept.Databind();

现在插入的记录都在GridView显示的罚款。在此之后我存储在我的数据库中的'StaffDepartments'表这个记录。我有3列在'Staffdepartments'表

  1.StaffId(PK  - 有链接与工作人员表)
2.DepartmentId(PK - 与Department表链接)
3.IsHOD。

在这里记录存储罚款在database.No问题将记录到数据库中。

我有一些问题在这里。

* 1.How可以检查中的DepartmentID是否在字典已经在那里加入它。
2.当我编辑的工作人员详细哪能通过检查的GridView行的复选框中删除从字典中选定的部门。(这里的记录从数据库里,所​​以当我点击删除按钮,记录应在数据库中删除以及)
的*

如果它是一个列表,而不是字典,我可以通过

获取中的DepartmentID

  INT =的DepartmentID(INT)gridDept.DataKeys [row.RowIndex] .Values​​ [的DepartmentID];

但在字典我不知道该怎么办与键​​和值对相同的....谁能帮我在这里。


解决方案

  

如何检查是否的DepartmentID已经存在的
  字典增加了它。


您的可能的使用:

如果(dict.Keys.Any(D = GT;!d.DepartmentId == department.DepartmentId))
    dict.Add(部门,chkHOD.checked);

但是,什么是错在这里。如果你真正的关键是的DepartmentID ,而不是(对象标识),你应该让它在字典中的关键。例如,您可以定义一个辅助类:

公共类DepartmentBindingHelper
{
    公众诠释{的DepartmentID获得;组; }
    公共部门部门{搞定;组; }
    公共BOOL经过{搞定;组; }
}

这是再这样定义的字典:

VAR字典=新词典< INT,DepartmentBindingHelper>();

和这种方式添加的对象字典:

如果(!dict.ContainsKey(department.DepartmentId))
    dict.Add(department.DepartmentId,新DepartmentBindingHelper
    {
        =的DepartmentID department.DepartmentId,
        部=部,
        选中= chkHOD.checked
    });

然后你可以只收藏价值绑定到网格:

gridDept.Datasource = dict.Values​​; //这是一个IEnumerable< D​​epartmentBindingHelper>
gridDept.Databind();

和您的code检索的DepartmentID 从行会的工作没有变化:

INT =的DepartmentID(INT)gridDept.DataKeys [row.RowIndex] .Values​​ [的DepartmentID];

I've created a Dictionary like

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

here Department is a class and I have Id,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

dict.Add(department,chkHOD.checked);

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

gridDept.Datasource=dict;
gridDept.Databind();

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)
3.IsHOD.

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.

解决方案

How can check whether the DepartmentId is already there in the Dictionary before adding to it.

You could use this:

if (!dict.Keys.Any(d => d.DepartmentId == department.DepartmentId))
    dict.Add(department,chkHOD.checked);

But something is wrong here. If your real key is the DepartmentId and not the Department (object identity) you should make it the key in the dictionary. For example, you could define a helper class:

public class DepartmentBindingHelper
{
    public int DepartmentId { get; set; }
    public Department Department { get; set; }
    public bool Checked { get; set; }
}

An then define a dictionary like this:

var dict = new Dictionary<int, DepartmentBindingHelper>();

And add the objects this way to the dictionary:

if (!dict.ContainsKey(department.DepartmentId))
    dict.Add(department.DepartmentId, new DepartmentBindingHelper
    {
        DepartmentId = department.DepartmentId,
        Department = department,
        Checked = chkHOD.checked
    });

Then you can bind only the value collection to the grid:

gridDept.Datasource = dict.Values;// it's an IEnumerable<DepartmentBindingHelper>
gridDept.Databind();

And your code to retrieve the DepartmentId from a row would work without changes:

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

这篇关于与GridView的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆