简体   繁体   English

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

[英]Error message “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index”

I have a DataGridView . 我有一个DataGridView I want to add column 4 values. 我想添加第4列的值。 I wrote this code. 我写了这段代码。 But it shows above error on Total += Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[4].Value); 但是它在Total += Convert.ToDecimal(dgvSalesFooterAdd.Rows[i].Cells[4].Value);上显示了以上错误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: 创建第五列代码:

            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();

C#'s arrays are zero-based. C#的数组从零开始。 This means the first element is element 0. The 4th element is element 3. 这意味着第一个元素是元素0。第四个元素是元素3。

You probably have only four columns, numbered 0 to 3. 您可能只有四列,编号为0到3。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 错误“索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引” - Error “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index” 单击有效错误:索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Error at Click evant: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 错误:索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 动态文本框错误索引超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Dynamic textboxes error Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM