0

我有一个奇怪的问题。将列表分配给数据源后。当我单击网格时,出现错误Index -1 does not have value。无论我在哪里尝试并捕获此错误都不会被捕获。

下面是将数据添加到列表并分配给 DataSource 的代码。

try
{
    this.GrdMain.DataSource = null;
    this.order.LineItems.Add(oli);
    this.GrdMain.DataSource = order.LineItems;
    this.FormatGrid();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "btnAddLine_Click");
}

下面是代码FormatGrid()

    private void FormatGrid()
    {
        try
        {
        for (int i = 0; i < this.GrdMain.Columns.Count; i++)
        {

            if (this.GrdMain.Columns[i].Name.ToUpper() == "ItemCode".ToUpper())
            {
                DataGridViewColumn column1 = this.GrdMain.Columns[i];
                column1.Width = 60;

            }
            else if (this.GrdMain.Columns[i].Name.ToUpper() == "ItemName".ToUpper())
            {
                DataGridViewColumn column1 = this.GrdMain.Columns[i];
                column1.Width = 200;

            }
            else if (this.GrdMain.Columns[i].Name.ToUpper() == "Disposable".ToUpper())
            {
                DataGridViewColumn column1 = this.GrdMain.Columns[i];
                column1.Width = 60;
                column1.HeaderText = "Disposable";


            }
            else if (this.GrdMain.Columns[i].Name.ToUpper() == "Rate".ToUpper())
            {
                DataGridViewColumn column1 = this.GrdMain.Columns[i];
                column1.Width = 70;

            }
            else if (this.GrdMain.Columns[i].Name.ToUpper() == "Qty".ToUpper())
            {
                DataGridViewColumn column1 = this.GrdMain.Columns[i];
                column1.Width = 70;

            }
            else if (this.GrdMain.Columns[i].Name.ToUpper() == "Total".ToUpper())
            {
                DataGridViewColumn column1 = this.GrdMain.Columns[i];
                column1.Width = 70;

            }
            else 
                GrdMain.Columns[i].Visible = false;

        }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message, "FormatGrid");
        }
    }
4

0 回答 0