WinForms DataGridView - 最后一行数据未显示

时间:2012-08-04 17:52:35

标签: c# winforms datagridview

我有一个DataGridView,VirtualMode设置为true,数据通过CellValueNeeded事件提供。一切都运行正常,但......最后一行没有数据。我在设计器中设置了列,其中有几列是链接列,这些列只显示没有数据。我验证了数据是在我的源对象中(我绑定到IEnumerable)并且RowCount设置正确。

如果我向源添加另一行,它将不会显示,但之前不可见的行将显示。

这是 NOT AllowUserToAddRows的问题 - 它被设置为false。该行只是空白,并且似乎没有为IEnumerable中的该项调用CellValueNeeded事件。

我正把头发拉出来。请帮忙。

gvPilots.VirtualMode = true;
gvPilots.AutoGenerateColumns = false;
gvPilots.AllowUserToAddRows = false;

IEnumerable<pilot> _pilotData = jamboree.pilots.Where(p => p.sync_state != (byte)SyncState.IsDeleted);
gvPilots.RowCount = _pilotData.Count();

private void gvPilots_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
        if (e.RowIndex == this.gvPilots.RowCount - 1) return;

        pilot thisPilot = null;

        thisPilot = this._pilotData.ElementAt(e.RowIndex);

        switch (this.gvPilots.Columns[e.ColumnIndex].Name)
        {
            case "columnId":
                e.Value = thisPilot.id;
                break;
            /* Bunch of case statements... */
            case "columnType":
                e.Value = Enum.GetName(typeof(PilotType), thisPilot.pilot_type);
                break;
        }
}

1 个答案:

答案 0 :(得分:0)

我刚刚回答了我自己的问题......我的CellValueNeeded事件的第一行阻止了数据的显示。复制&amp;粘贴FTW。

相关问题