【发布时间】:2011-11-21 04:44:10
【问题描述】:
在虚拟模式下使用网格视图时,我尝试编辑标题单元格值,但无法正常工作。
在我的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;
}
}
【问题讨论】:
-
您能否提供更多代码 - 我刚刚尝试在虚拟模式下(从 CellValueNeeded 内部和其他事件期间)设置 HeaderCell 值,它工作正常。
-
我添加了完整的 CellValueNeeded 方法,但我没有看到任何其他会影响此的方法
-
这可能是一个愚蠢的问题,但你确定 RecordNumber 是字符串类型的吗?不会显示整数。
标签: winforms datagridview