我想在虛擬模式下使用網格視圖編輯標題單元格的值,但我無法讓它工作。如何在虛擬模式下設置Winforms DataGridView的行標題單元格值?
在我CellValueNeeded
事件中,我有以下代碼:
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
不幸的是,我的頭細胞仍然顯示爲空白。
如何獲取顯示的行標題值?最終的結果是我需要我的行來顯示它們的行號,以更好地跟蹤數據。
更新:完整的事件代碼如下:
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;
}
}
你能否提供更多的代碼 - 我只是想設置HeaderCell值在虛擬模式(從內外CellValueNeeded和在其他事件),它工作正常時。 –
我添加了完整的CellValueNeeded方法,但我沒有看到任何其他方法會影響此 – KallDrexx
這可能是一個愚蠢的問題,但是您確定RecordNumber的類型是字符串嗎?整數不會顯示。 –