DataGridView CheckBox问题

时间:2009-12-26 17:23:47

标签: c# datagridview

嘿伙计们,我正在编写一个应用程序来检索从foreach循环填充的每个NT组的组名和访问权限。另外,我已经包含了一个DataGridView控件,其中每个单元格都有一个复选框列,应用程序将相应地检查每个单元格,例如每个组的读取,写入,修改等。我不能为我的生活,弄清楚如何相应地检查这些盒子。下面的代码片段演示了我尝试使用标准DataGridView控件文本框列进行的操作,但我想创建这些复选框而不是文本框。任何反馈将不胜感激。在下面的代码片段中,Property是从另一个方法传入的路径。

 private void CheckDirPermissions(ResultProperty Property)
    {
        if (Property.Type == typeof(string) && !Property.IsArray)
        {
            try
            {
                FileSecurity folderSecurity = File.GetAccessControl(Property.String);
                foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                {



                    string IdentityReference = fileSystemAccessRule.IdentityReference.ToString();
                    string AccessControlType = fileSystemAccessRule.AccessControlType.ToString();
                    string filesystemrights = fileSystemAccessRule.FileSystemRights.ToString();
                    string IsInherited = fileSystemAccessRule.IsInherited.ToString();




                    DataGridDirPermissions.Rows.Add(IdentityReference,
                                                    filesystemrights,                                       
                                                    AccessControlType,
                                                    IsInherited);

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Path does not exist.", "Path Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else return;
    }

1 个答案:

答案 0 :(得分:1)

您必须定义类型为DataGridViewCheckBoxColumn的DataGridView的读取,写入,修改等列。然后,当您读取每个组的权限时,您可以获得每个权限的相应布尔值,并使用以下值创建行:

foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
    string IdentityReference = fileSystemAccessRule.IdentityReference.ToString();

    AccessControlType accessControlType = fileSystemAccessRule.AccessControlType;
    FileSystemRights filesystemrights = fileSystemAccessRule.FileSystemRights;
    bool isInherited = fileSystemAccessRule.IsInherited;

    // .. Get specific permissions ...

    bool allowControlType = accessControlType == AccessControlType.Allow;
    bool canRead = (filesystemrights & FileSystemRights.Read) == FileSystemRights.Read;
    bool canWrite = (filesystemrights & FileSystemRights.Write) == FileSystemRights.Write;
    bool canExecute = (filesystemrights & FileSystemRights.ExecuteFile) == FileSystemRights.ExecuteFile;

    // ... Any more specific permissions ...

    dataGridView1.Rows.Add(IdentityReference, allowControlType, canRead, canWrite, canExecute, ... );
}

这样你的DataGridView将在第一个单元格(作为TextBox)和每个特定权限的CheckBox中具有Group Name,例如:

Everyone                    (check)      (check)     (no check)   (no check)
BUILTIN\Administrators      (check)      (check)     (check)      (check)
BUILTIN\Users               (check)      (check)     (check)      (no check)

依旧......