0

我试图从 SQLite 数据库中提取数据,第一个 DGV 加载得很好,但第二个没有。调试时,我得到这个:

-Current 'this.specListing.Current' 引发了类型为 'System.IndexOutOfRangeException' 对象 {System.IndexOutOfRangeException} 的异常

this.specListing 是一个 BindingSource。更具体的错误是“索引 -1 没有值”,并且有 2 项(计数:2)

代码是:

public frmClassEditor()
{
    //start listing
    InitializeComponent();
    this.clsList = new List<SQLiteDataPair>();
    this.specList = new List<pilotSpec>();

    //create binding sources
    this.clsListing = new BindingSource();
    this.specListing = new BindingSource();

    //updated lags
    this.updatedSpec = true;

    //update listing
    updatePilotListing();
    this.clsListing.DataSource = this.clsList;
    this.specListing.DataSource = this.specList;

    dgvPilotListing.DataSource = this.clsListing;
    dgvPilotSpec.DataSource = this.specListing;

    refreshDGVPilot();
    }

private void refreshDGVSpec()
    {
    this.specListing.ResetBindings(false);
    if (dgvPilotSpec.Columns.Count > 0)
       {
            //do nothing right now
       }
    }

private void updatePilotSpecLst(SQLiteConnection conn, long classID)
{
    this.specList.Clear();
    SQLiteCommand getPilotSpec = conn.CreateCommand();
    getPilotSpec.CommandText = "SELECT * FROM classSpec WHERE classID = " + classID;
    SQLiteDataReader rdr = getPilotSpec.ExecuteReader();
    while (rdr.Read())
    {
       this.specList.Add(new pilotSpec(rdr.GetInt64(0), rdr.GetString(1), txtClassName.Text, rdr.GetInt64(2)));
    }

    refreshDGVSpec();
    }

PilotSpec 非常简单:

public class pilotSpec
{
    public long pilotSpecID;
    public string pilotClassName;
    public string pilotSpecName;
    public long pilotSpecLevel;

    public pilotSpec(long id, string name, string className, long level)
    {
        this.pilotSpecID = id;
        this.pilotClassName = className;
        this.pilotSpecName = name;
        this.pilotSpecLevel = level;
    }
}

我想不通。

4

1 回答 1

1

您可以将刷新网格方法更新为此:

private void refreshDGVSpec()
{
    CurrencyManager currencyManager = (CurrencyManager)this.BindingContext[dgvPilotSpec.DataSource];
    currencyManager.Refresh();
}

编辑:您的班级成员需要具有公共属性:

更改 public long pilotSpecID; public long PilotSpecID { get; set; }

对所有公共成员变量执行相同的操作。

于 2013-09-10T04:46:19.250 回答