当前位置:编程学习 > C#/ASP.NET >>

本段代码错在哪了?

本来所有数据都应该不显示,可到最后还是显示几条。(有五条数据,因为满足等于0,不显示,剩下4行后,再执行,应该都不显示,最后显示2条)。
int j = 0;
            while(j<dataGridView1.Rows.Count)
            {
                
                if (int.Parse(dataGridView1.Rows[j].Cells[3].Value.ToString()) == 0)
                {
                    dataGridView1.Rows.RemoveAt(j);
                }
                else
                {
                    int safe = int.Parse(ws.tqsj("select expiration from goods where goodsname='" + dataGridView1.Rows[j].Cells[0].Value.ToString() + "' and goodstype='" + dataGridView1.Rows[j].Cells[1].Value.ToString() + "'"));
                    string scrq = dataGridView1.Rows[j].Cells[2].Value.ToString();
                    DateTime scrq1 = Convert.ToDateTime(scrq);
                    DateTime b = DateTime.Now;

                    if (b.Day < scrq1.Day)
                    {
                        scrq1 = scrq1.AddMonths(-1);
                    }
                    int iM = (b.Year * 12 + b.Month) - (scrq1.Year * 12 + scrq1.Month);
                    int safe1 = safe - 2;
                    if (iM >= safe)
                    {
                        dataGridView1.Rows.RemoveAt(j);
                    }
                    else
                    {
                        if (iM <= safe1)
                        {
                            dataGridView1.Rows.RemoveAt(j);
                        }
                    }
                }
                j++;
            } --------------------编程问答-------------------- 再强调一次,删除,要倒着删
while(j>=0)
{
。。。。。。
j--;
} --------------------编程问答-------------------- 提示:索引超出范围。必须为非负值并小于集合大小。 --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
再强调一次,删除,要倒着删
while(j>=0)
{
。。。。。。
j--;
}


正解! --------------------编程问答-------------------- 那int j=? --------------------编程问答-------------------- 骚年 这么简单的问题 你应该学会debug 单步调试 很容易发现问题 要学会调试 哎 好久不上这个论坛了 8年过去了 加油吧 一个还在做技术的人 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
再强调一次,删除,要倒着删
while(j>=0)
{
。。。。。。
j--;
}


+1

删了一个就少了一个。。 --------------------编程问答-------------------- 我现在改成这样,提示索引超出范围。必须为非负值并小于集合大小。
参数名: index
int k=dataGridView1.Rows.Count;
            while (k > 0)
            {
                int safe = int.Parse(ws.tqsj("select expiration from goods where goodsname='" + dataGridView1.Rows[k].Cells[0].Value.ToString() + "' and goodstype='" + dataGridView1.Rows[k].Cells[1].Value.ToString() + "'"));//就是这句话
                string scrq = dataGridView1.Rows[k].Cells[2].Value.ToString();
                DateTime scrq1 = Convert.ToDateTime(scrq);
                DateTime b = DateTime.Now;

                if (b.Day < scrq1.Day)
                {
                    scrq1 = scrq1.AddMonths(-1);
                }
                int iM = (b.Year * 12 + b.Month) - (scrq1.Year * 12 + scrq1.Month);
                int safe1 = safe - 2;
                if (iM > safe && iM < safe1)
                {
                    dataGridView1.Rows.RemoveAt(k);
                }
                k--;
            }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,