|
I want to confirm that this snippet is in line with best-practice methods for building a VirtualMode=true datagrid, and solicit opinions on whether a customized DataGridViewCheckBoxColumn with better VirtualMode support seems like a useful enhancement.
In Form_Load, I call this configuration method pair private DataGridViewColumn GetChkColumn(string name, int width) { DataGridViewCheckBoxColumn result = new DataGridViewCheckBoxColumn(); result.Name = result.HeaderText = result.DataPropertyName = name; result.Width = width; result.ValueType = typeof(bool); return result; }
private void ConfigureItemGrid() {
DataGridViewColumn doneCol, ideaCol, maybeCol, holdCol, nextCol, minorCol, nowCol, asapCol; DataGridViewTextBoxColumn subjectCol; DataGridViewCellStyle overdueSubjectStyle = new DataGridViewCellStyle(); DataGridViewCellStyle doneSubjectStyle = new DataGridViewCellStyle(); doneCol = GetChkColumn("Done", 43); ideaCol = GetChkColumn("Idea", 38); maybeCol = GetChkColumn("Maybe", 50);
holdCol = GetChkColumn("Hold", 39);
nextCol = GetChkColumn("Next", 39);
minorCol = GetChkColumn("Minor", 44);
nowCol = GetChkColumn("Now", 38);
asapCol = GetChkColumn("ASAP", 45);
subjectCol = new DataGridViewTextBoxColumn(); subjectCol.Name = subjectCol.HeaderText = subjectCol.DataPropertyName = "Subject"; subjectCol.Resizable = DataGridViewTriState.False; subjectCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
grdItems.Columns.AddRange(doneCol, ideaCol, maybeCol, holdCol, nextCol, minorCol, nowCol, asapCol, subjectCol); }
Then, I manipulate a DataSet to retrieve a DataView, and call this method with the result
private void BindControls(DataView view) { projectItemBindingSource.DataSource = view; int dataRowCount = view.Count; grdItems.RowCount = dataRowCount; }
Cell data is retrieve by a simple CellValueNeeded handler:
private void grdItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { if (e.RowIndex >= projectItemBindingSource.Count) { return; } if (grdItems.Columns[e.ColumnIndex] is DataGridViewButtonColumn) return; dsProjectItem.ProjectItemRow row = (dsProjectItem.ProjectItemRow)((DataRowView)projectItemBindingSource[e.RowIndex]).Row; e.Value = row[grdItems.Columns[e.ColumnIndex].DataPropertyName]; }
If I change out the checkbox column for a TextBox column in the factory method (GetChkColumn), render time on 2500 rows is about half the time required with the CheckBox. I spoke to a couple of MS guys about a related issue I was having with the checkbox, and having done the 'randomly hit break in the IDE and examine the callstack' level of investigation of the underlying process, it appears that the virtual mode DGV is creating all 20,000 checkbox controls, despite a maximum of (still a lot of controls, but) about 200 visible in the DGV at a time. 99% resource overkill due to a 'simple editing' scenario instead of a complex editing scenario?
|