0
我收到错误当我从数据表中删除记录,唯一的例外是下面Windows窗体(C#):DataGridView的System.IndexOutOfRangeException
> System.IndexOutOfRangeException: Index 10 does not have a value. at
> System.Windows.Forms.CurrencyManager.get_Item(Int32 index) at
> System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32
> rowIndex)
我使用的DataTable填充数据和BindingSource的绑定的DataGridView,当我在那个时候异常发生上述删除数据表中的行记录,下面是我的代码
DataTable journeyDataTable = new DataTable();
private void ReceivedMessage(object sender, MqttMsgPublishEventArgs e)
{
var journeyBindingSource = new BindingSource();
var result = System.Text.Encoding.UTF8.GetString(e.Message);
switch (e.Topic)
{
case MqttTokens.JourneyHeaderInfo:
title = "Journey Header Info";
journeyDataTable.Rows.Add(new object[] { title, result });
break;
case MqttTokens.JourneyDetail:
title = "Journey Detail Info";
journeyDataTable.Rows.Add(new object[] { title, result });
break;
case MqttTokens.JourneyProgress:
title = "Journey Progress Info";
journeyDataTable.Rows.Add(new object[] { title, result });
break;
default:
break;
}
//Add LogDetail
infoLogger.Info(title + result);
if (journeyDataTable.Rows.Count > 0)
{
journeyBindingSource.DataSource = journeyDataTable;
this.Invoke((MethodInvoker)delegate()
{
grdJourneyInformation.DataSource = journeyBindingSource;
});
}
if (journeyDataTable.Rows.Count > 10)
{
var dataTableRow = journeyDataTable.Rows.Cast<DataRow>().Reverse().Skip(10);
if (dataTableRow.FirstOrDefault() != null)
dataTableRow.FirstOrDefault().Delete();
}
}
我试着用的CurrencyManager referecing,在线程中删除数据等。但同样的例外是发生
请帮
谢谢
@ roty.ap:感谢您的回答,但是这个问题的解决方案是什么?任何想法我不清楚如何解决 – MansinhDodiya
将其更改为8或更少 –
@ roty.ap:这是示例,但在我的项目中它的可配置...值大约是500 – MansinhDodiya