C# 错误消息“索引超出范围。必须为非负且小于集合的大小。参数名称:索引”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18749055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:06:17  来源:igfitidea点击:

Error message "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

c#winformsdatagridview

提问by Animesh Ghosh

I have a DataGridView. I want to add column 4 values. I wrote this code. But it shows above error on Total += Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[4].Value);.

我有一个DataGridView. 我想添加第 4 列的值。我写了这段代码。但它在Total += Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[4].Value);.

 decimal Total = 0;

 for (int i = 0; i < dgvSalesFooterAdd.Rows.Count; i++)
 {
      dgvSalesFooterAdd.Rows[i].Cells[4].Value = 
          Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[3].Value) * 
          Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[3].Value);

          Total += Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[4].Value);
 }
 lblFinalTotalAdd.Text = Total.ToString();

Creating 5th column code:

创建第 5 列代码:

            OleDbConnection con = new OleDbConnection(conn);
            con.Open();
            DataTable dtusers = new DataTable();
            OleDbCommand cmd = new OleDbCommand("Select Shorts,Code,Description,Percentage from SalesFields", con);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dtusers);
            dgvSalesFooterAdd.DataSource = dtusers;
            dgvSalesFooterAdd.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            dgvSalesFooterAdd.Columns[0].Name = "Shorts";
            dgvSalesFooterAdd.Columns[1].Name = "Code";
            dgvSalesFooterAdd.Columns[2].Name = "Description";
            dgvSalesFooterAdd.Columns[3].Name = "Percentage";
            dgvSalesFooterAdd.Columns[0].HeaderText = "Shorts";
            dgvSalesFooterAdd.Columns[1].HeaderText = "Code";
            dgvSalesFooterAdd.Columns[2].HeaderText = "Description";
            dgvSalesFooterAdd.Columns[3].HeaderText = "Percentage";
            DataGridViewColumn amount = new DataGridViewColumn();
            amount.HeaderText = "Amount";
            amount.Name = "Amount";
            amount.CellTemplate = new DataGridViewTextBoxCell();
            dgvSalesFooterAdd.Columns.Insert(4, amount);
            con.Close();

            con.Close();

回答by Bart Friederichs

C#'s arrays are zero-based. This means the first element is element 0. The 4th element is element 3.

C# 的数组是从零开始的。这意味着第一个元素是元素 0。第四个元素是元素 3。

You probably have only four columns, numbered 0 to 3.

您可能只有四列,编号为 0 到 3。

回答by 123 456 789 0

Try this,

尝试这个,

 decimal Total = 0;

 for (int i = 0; i < dgvSalesFooterAdd.Rows.Count; i++)
 {
      dgvSalesBodyAdd.Rows[i].Cells[3].Value = 
          Convert.ToDecimal(dgvSalesBodyAdd.Rows[i].Cells[2].Value) * 
          Convert.ToDecimal(dgvSalesBodyAdd.Rows[i].Cells[2].Value);

          Total += Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[3].Value);
 }
 lblFinalTotalAdd.Text = Total.ToString();