我有一个奇怪的问题。将列表分配给DataSource后。当我点击网格时,出现错误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");
}
}