DevExpress XtraGrid Control with checkBoxEdit column

陌路散爱 提交于 2019-12-01 09:54:01

问题


I've a DevExpress XtraGrid control with three columns and a unbound checkBoxEdit column for users to select when deleting items from the grid. I'm able to add the checkBoxEdit on the xtraGrid. However, i don't have any idea as to how i can get the selected lists' primary key(s) to be deleted. Any idea is highly appreciated. Thanks


回答1:


I believe you can use the following approach:

void InitGrid() {
    gridControl1.DataSource = new List<Person> { 
        new Person(){ ID = 0 }, 
        new Person(){ ID = 1 }, 
        new Person(){ ID = 2 }
    };
    gridView.Columns["ID"].Visible = false;
    gridView.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
    {
        UnboundType = DevExpress.Data.UnboundColumnType.Boolean,
        Caption = "Mark as Deleted",
        FieldName = "IsDeleted",
        Visible = true,
    });
}
IDictionary<int, object> selectedRows = new Dictionary<int, object>();
void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
    int id = (int)gridView.GetListSourceRowCellValue(e.ListSourceRowIndex, gridView.Columns["ID"]);
    if(e.IsGetData) 
        e.Value = selectedRows.ContainsKey(id);
    else {
        if(!(bool)e.Value)
            selectedRows.Remove(id);
        else selectedRows.Add(id, e.Row);
    }
}
void OnDelete(object sender, System.EventArgs e) {
    //... Here you can iterate thought selectedRows dictionary
}
//
class Person {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
}

Related help topics:

  • ColumnView.CustomUnboundColumnData Event
  • ColumnView.GetListSourceRowCellValue Method


来源:https://stackoverflow.com/questions/10347505/devexpress-xtragrid-control-with-checkboxedit-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!