i'm using ObjectListview to display checkboxes for columns but there is a problem.
My model is like this:
public class HocVienLopDTO
{
public HocVienDTO HocVien { get; set; }
public double Diem { get; set; }
public List<NgayHocDTO> DSNgayHoc { get; set; }
}
public class NgayHocDTO
{
public DateTime Ngay { get; set; }
public bool CoHoc { get; set; }
}
I want to create a listview like this: (Diem, DSNgayHoc[0], DSNgayHoc[1], ...)
. And i want to use checkbox for all the DSNgayHoc column to present value of it's CoHoc property. So i dynamically generate columns like this:
this.lstvDiemDanh.UseSubItemCheckBoxes = true;
List<OLVColumn> colList = new List<OLVColumn>();
for (int i = 0; i < this.lop.DSNgayHoc.Count; i++)
{
OLVColumn col = new OLVColumn();
col.IsHeaderVertical = true;
col.CheckBoxes = true;
col.AspectName = string.Format(string.Format("DSNgayHoc[{0}].CoHoc", i));
col.Text = this.lop.DSNgayHoc[i];
col.Width = 20;
col.IsEditable = true;
colList.Add(col);
}
this.lstvDiemDanh.AllColumns.AddRange(colList);
this.lstvDiemDanh.RebuildColumns();
All the checkbox was displayed fine but their state is not changed when i clicked them. (Always square box). I tried to handle ChangingSubItem event to change the currentValue and newValue but no luck. Please help!
Sorry about my english.
The OLV is using reflection to search for a property with the AspectName
name. This won't work in this case, because it does not know that you are accessing a list index.
Instead of using the AspectName
// ...
col.AspectName = string.Format(string.Format("DSNgayHoc[{0}].CoHoc", i));
// ...
you have to use the AspectGetter
and AspectPutter
callbacks to access the DSNgayHoc
list as desired.
// ...
int listIndex = i;
col.AspectGetter = delegate(object rowObject) {
HocVienLopDTO model = rowObject as HocVienLopDTO;
if (model.DSNgayHoc.Count > listIndex)
return model.DSNgayHoc[listIndex].CoHoc;
else
return false;
};
col.AspectPutter = delegate(object rowObject, object value) {
HocVienLopDTO model = rowObject as HocVienLopDTO;
if (model.DSNgayHoc.Count > listIndex)
model.DSNgayHoc[listIndex].CoHoc = (bool)value;
};
// ...