例外没有。索引超出了范围。必须是非负的,并且小于收集参数名称的大小:索引
每次我单击任何单元格时,这都是弹出的错误。 这是我的代码的副本。怎么了?有什么建议吗?
private void CarsdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.CarsdataGridView.DefaultCellStyle.SelectionBackColor = selectionBackColor;
this.CarsdataGridView.DefaultCellStyle.SelectionForeColor = selectionForeColor;
var selectedCarData = CarsdataGridView.SelectedRows[0].DataBoundItem as CarsData;
if (selectedCarData != null)
{
txtId.Text = CarsdataGridView.SelectedRows[0].Cells[0].Value.ToString();
txtModel.Text = CarsdataGridView.SelectedRows[0].Cells[1].Value.ToString();
txtYear.Text = CarsdataGridView.SelectedRows[0].Cells[2].Value.ToString();
txtGearBox.Text = CarsdataGridView.SelectedRows[0].Cells[3].Value.ToString();
txtColor.Text = CarsdataGridView.SelectedRows[0].Cells[4].Value.ToString();
txtMax_Speed.Text = CarsdataGridView.SelectedRows[0].Cells[5].Value.ToString();
}
}
Every time I click any cell this is the error that pops up.
Here is a copy of my code. What's wrong with it? Any suggestions?
private void CarsdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.CarsdataGridView.DefaultCellStyle.SelectionBackColor = selectionBackColor;
this.CarsdataGridView.DefaultCellStyle.SelectionForeColor = selectionForeColor;
var selectedCarData = CarsdataGridView.SelectedRows[0].DataBoundItem as CarsData;
if (selectedCarData != null)
{
txtId.Text = CarsdataGridView.SelectedRows[0].Cells[0].Value.ToString();
txtModel.Text = CarsdataGridView.SelectedRows[0].Cells[1].Value.ToString();
txtYear.Text = CarsdataGridView.SelectedRows[0].Cells[2].Value.ToString();
txtGearBox.Text = CarsdataGridView.SelectedRows[0].Cells[3].Value.ToString();
txtColor.Text = CarsdataGridView.SelectedRows[0].Cells[4].Value.ToString();
txtMax_Speed.Text = CarsdataGridView.SelectedRows[0].Cells[5].Value.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会遇到错误,因为没有像您尝试在SelecteDrows或单元格中导入的索引那样多的数据,并添加检查以查看数据是否存在,然后才能获得数据可以解决问题。
You may be getting an error because there is not as much data as the index you tried to import in SelectedRows or Cells, adding a check to see if the data exists before you get the data can solve the problem.