简体   繁体   English

数据源已绑定到DataGridView时将字符串列表添加到DataGridView

[英]c# - Add list of strings to the DataGridView when the DataSource is already bonded to DataGridView

I am working on a project where I need to develop a functionality to assign/modify/remove Roles to the User 我正在一个项目中,我需要开发一个功能来向用户分配/修改/删除角色

I have a list of User Roles attached to the DataGridView's DataSource as below. 我有一个附加到DataGridView的DataSource的用户角色列表,如下所示。

dgvAssignedRoles.DataSource = _userBll.ReadUserRoles(userId);

Which looks like below: 如下图所示:

UserRoles

I would like to add few more roles (like eg, HR_Admin, Manager_Role, EndUser_Role, etc.) to the user, which will be fetched from list of Roles from another form 我想向用户添加更多角色(例如HR_Admin,Manager_Role,EndUser_Role等),这些角色将从另一种形式的“角色”列表中获取

RoleList Form - RoleList表格-

// Global Declaration
public List<string> SeletedRoleList { get; set; }

// Inside the Add function
SeletedRoleList = new List<string>();
foreach (DataGridViewRow row in dgvDataList.Rows)
{
    if (row.Selected)
    {
        string str = row.Cells[0].Value.ToString();
        SeletedRoleList.Add(str);
    }
}

And the I am trying to add these SelectedRoleList to DataGridView which already have some roles, but it is throwing an error below. 而且我正在尝试将这些SelectedRoleList添加到已经具有某些角色的DataGridView中,但是它在下面引发了错误。

User Form - 用户表格-

        var rolelistform = new RoleList();
        rolelistform.ShowDialog();

        foreach (var v in rolelistform.SeletedRoleList)
        {
            dgvAssignedRoles.Rows.Add(v);
        }

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound. 当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的rows集合中。

I know once the DataSource property is used to bind to data you cannot explicitly add rows directly to the DataGridView. 我知道一旦使用DataSource属性绑定到数据,就无法将行直接显式添加到DataGridView。

Anyone have an idea how could I achieve this and add the rows explicitly to DataSource property. 任何人都有一个主意,我该如何实现并将行显式添加到DataSource属性。

Thanks! 谢谢!

        DataTable DT = (DataTable)dgvAssignedRoles.DataSource;
        DataRow DR = DT.NewRow();
        DR[0] = "your Roles";
        DT.Rows.Add(DR);

after binding datasource Cast into DataTable and add new Rows 将数据源绑定到数据表并添加新行后

Finally I figured out the best solution by myself. 最后,我自己找出了最佳解决方案。

In User form, declared a global List<string> variable 在用户窗体中,声明一个全局List<string>变量

// Global Declaration    
private static List<string> AssignedRoleList { get; set; }

Then assigned the existed user roles to this global variable AssignedRoleList and bounded the user roles data to DataGridView. 然后将现有的用户角色分配给此全局变量AssignedRoleList并将用户角色数据绑定到DataGridView。 Below is the code. 下面是代码。

AssignedRoleList = new List<string>();
AssignedRoleList = _userBll.ReadUserRoles(userId);
if (AssignedRoleList.Count > 0)
{
    var source = new BindingSource {DataSource = AssignedRoleList.Select(x => new {Roles = x})};
    dgvAssignedRoles.DataSource = source;
}

Then after I added the new SelectedRoleList to AssignedRoleList and bounded it to DataGridView. 然后,在将新的SelectedRoleList添加到AssignedRoleList并将其绑定到DataGridView之后。

NewRoleList = new List<string>();
// Assigned Old user roles to the new one.
if (AssignedRoleList != null) NewRoleList = AssignedRoleList;

foreach (var v in dataviewform.SeletedDataList)
{
    bool status = true;
    // Check if user already have the roles
    if (dgvAssignedRoles.Rows.Cast<DataGridViewRow>().Any(row => Equals(row.Cells[0].Value, v)))
    {
        MessageBox.Show("Role already exist.");
        status = false;
    }
    // Adding the new selected roles to the existence user role if not present
    if (status)
        NewRoleList.Add(v);
    }
    // Finally attaching the both new and old user roles to datagridview
    var source = new BindingSource {DataSource = NewRoleList.Select(x => new {Roles = x})};
    dgvAssignedRoles.DataSource = source;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM