例外没有。索引超出了范围。必须是非负的,并且小于收集参数名称的大小:索引

发布于 2025-02-03 04:15:12 字数 1051 浏览 3 评论 0原文

每次我单击任何单元格时,这都是弹出的错误。 这是我的代码的副本。怎么了?有什么建议吗?

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2025-02-10 04:15:12

您可能会遇到错误,因为没有像您尝试在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文