I am trying edit the header cell value when using a grid view in virtual mode, but I can’t get it working.
In my CellValueNeeded event I have the following code:
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
Unfortunately, my header cells are still displaying blank.
How can I get the row header values to display? The end result is I need my rows to show what row number they are, to better keep track of data.
Update: The full event code is below:
private void grvResults_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
const int BATCH_REQUEST_SIZE = 50;
int row = e.RowIndex + 1;
LogRecord record = null;
// Check if this record for this row is cached
if (!_recordCache.ContainsKey(row))
{
// Retrieve the requested record + a batch more to speed up operations and lessen queries
int page = (row / BATCH_REQUEST_SIZE) + 1;
var records = _currentStore.GetFilteredRecords(_filters, _currentSessionId, BATCH_REQUEST_SIZE, page);
if (records.Count > 0)
{
// Add all the records to the cache
for (int x = 0; x < records.Count; x++)
if (!_recordCache.ContainsKey(row + x))
_recordCache.Add(row + x, records[x]);
}
}
// Get the record from the cache
record = _recordCache[row];
if (record != null)
{
// Set the header cell to the record number
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
// Match the cell to the field
string cellFieldName = grvResults.Columns[e.ColumnIndex].Name;
if (record.Fields.Any(x => x.FieldName.Equals(cellFieldName, StringComparison.CurrentCultureIgnoreCase)))
e.Value = record.Fields.Where(x => x.FieldName.Equals(cellFieldName, StringComparison.CurrentCultureIgnoreCase)).Single().StringValue;
}
}
In the brief experiments I’ve done the HeaderCell.Value always shows in the row header, regardless of where it was set or whether the grid was in virtual mode.
One possibility is that the row header is too narrow to see the value.
Another is that RecordNumber is not a string – the Value displayed in the row header must be a string.