简体   繁体   English

InvalidArgument =值'3'对于'索引'无效。 参数名称:索引

[英]InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index

I have this error in my code: InvalidArgument=Value of'3' is not valid for 'index'. 我的代码中出现此错误:InvalidArgument =值“ 3”对“索引”无效。 parameter name: index , when i want to fill just part of row of my listview ! 参数名称:index,当我只想填充listview行的一部分时! this is my code: 这是我的代码:

 MySqlCommand cmd = new MySqlCommand("select acc_Id,acc_Name,acc_Place from accounts where archived=0 and  cus_Sup=" + 0 , objConn);
                    MySqlDataReader dataReader = cmd.ExecuteReader();
                    int i = 0;
                    while (dataReader.Read())
                    {
                        if (i % 2 == 0)
                            listView2.Items.Add(dataReader.GetValue(0).ToString()).BackColor = Color.Lavender;
                        else
                            listView2.Items.Add(dataReader.GetValue(0).ToString());

                        listView2.Items[i].SubItems.Add(dataReader.GetString(1).ToString());

                        listView2.Items[i].SubItems.Add((dataReader.IsDBNull(2) ? "No place added" : dataReader.GetString(2)));
                        //------------------------------------I meant here !
                        listView2.Items[i].SubItems.Add("0");
                        listView2.Items[i].SubItems.Add("0");
                        listView2.Items[i].SubItems.Add("0");

                    }
                    dataReader.Close();


                    //-------------load bills
                    for (int x = 0; x < listView2.Items.Count; x++)
                    {
                        cmd = new MySqlCommand("select c.acc_Id,a.bill_Number,a.bill_Date,c.acc_Name,Sum( (b.de_Quantity * b.de_Price) - ((b.de_Quantity * b.de_Price)*b.de_DisRate/100) ),a.bill_TaxRate,a.bill_DisRate,a.bill_Note,c.acc_Place from (accounts c right JOIN bills a on c.acc_Id = a.bill_From) LEFT JOIN  bill_Details b on a.bill_Id = b.bill_Id where c.acc_Id = " + listView2.Items[x].SubItems[0].Text + " group by a.bill_Id,a.bill_Number,a.bill_Date,c.acc_Name,a.bill_TaxRate,a.bill_DisRate,a.bill_Note,c.acc_Place ", objConn);
                        dataReader = cmd.ExecuteReader();

                        decimal totalSum = 0, summBills = 0;
                        i = 0;
                        while (dataReader.Read())
                        {

                            totalSum = decimal.Parse(dataReader.IsDBNull(4) ? "0" : dataReader.GetDecimal(4).ToString("n2"));
                            totalSum -= (totalSum * decimal.Parse(dataReader.IsDBNull(6) ? "0" : dataReader.GetDecimal(6).ToString("n2")) / 100);
                            totalSum += (totalSum * decimal.Parse(dataReader.IsDBNull(5) ? "0" : dataReader.GetDecimal(5).ToString("n2")) / 100);
                            summBills += totalSum;
                            i++;
                        }
                        //MessageBox.Show(dataReader.GetValue(0).ToString()+"   "+summBills.ToString());
                        listView2.Items[x].SubItems[3].Text = summBills.ToString("n2");// the error here !

                        dataReader.Close();

                    }

it's print the value of first record just ! 它只是打印第一条记录的值! , when it move to the second the error appear ! ,当它移到第二个位置时出现错误!

You've added 3 sub-items to each ListView Item. 您已经为每个ListView项目添加了3个子项目。 They will be Items[x].SubItems[0] to SubItems[2]. 它们将是Item [x] .SubItems [0]到SubItems [2]。 You would need to have added 4 SubItems to be able to use SubItem[3]. 您需要添加4个SubItem才能使用SubItem [3]。

Edit: 编辑:

When you're adding items in the top loop, you're not incrementing i - you need to change the top loop to: 在顶部循环中添加项目时,您无需增加i-您需要将顶部循环更改为:

            int i = 0;
            while (dataReader.Read())
            {
                if (i % 2 == 0)
                    listView2.Items.Add(dataReader.GetValue(0).ToString()).BackColor = Color.Lavender;
                else
                    listView2.Items.Add(dataReader.GetValue(0).ToString());

                listView2.Items[i].SubItems.Add(dataReader.GetString(1).ToString());

                listView2.Items[i].SubItems.Add((dataReader.IsDBNull(2) ? "No place added" : dataReader.GetString(2)));
                //------------------------------------I meant here !
                listView2.Items[i].SubItems.Add("0");
                listView2.Items[i].SubItems.Add("0");
                listView2.Items[i].SubItems.Add("0");

                i++;
            }
            dataReader.Close();

Without that, you're adding a new Item to the ListView each time through, but the SubItems are being added every time to listView2.Items[0]. 否则,您每次都会在ListView中添加一个新Item,但是SubItems每次都会添加到listView2.Items [0]。 Which is why is fails on the second one. 这就是为什么第二个失败。 The Items[0] has some multiple of 5 subitems, and the rest have none. Items [0]具有5个子项的某些倍数,其余子项均没有。

暂无
暂无

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

相关问题 InvalidArgument=&#39;-1&#39; 的值对 &#39;index&#39; 无效。 参数名称:索引 - InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index InvalidArgument =值&#39;6&#39;对于索引无效。 参数名称:索引 - InvalidArgument=Value of '6' is not valid for index. Parameter name: index invalidargument =值&#39;8&#39;对于&#39;索引&#39;无效 - invalidargument=value of '8' is not valid for 'index' InvalidArgument =值&#39;4&#39;对于&#39;索引&#39;无效 - InvalidArgument=Value of '4' is not valid for 'index' InvalidArgument =值&#39;1&#39;对&#39;索引&#39;无效 - InvalidArgument=Value of '1' is not valid for 'index' “InvalidArgument=‘1’的值对‘index’无效。 参数名称:index”,显示第二个数据索引并插入数据库时出现 - “InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index”, occurs when the second data index is displayed and nserted to database 附加信息:InvalidArgument =值“ 0”对“索引”无效 - Additional information: InvalidArgument=Value of '0' is not valid for 'index' c#InvalidArgument =值&#39;-1&#39;对&#39;索引&#39;无效 - c# InvalidArgument=Value of '-1' is not valid for 'index' ListView中的错误:InvalidArgument =值&#39;0&#39;对&#39;index&#39;无效 - Error in ListView: InvalidArgument = Value of '0' is not valid for 'index' Listview.count-InvalidArgument =值“ 0”对“索引”无效 - Listview.count - InvalidArgument=Value of '0' is not valid for 'index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM