PDA

View Full Version : سوال: خطای IndexOutOfRange



adineh67
یک شنبه 07 دی 1393, 13:34 عصر
سلام به همگی
یه قسمت از کدم خطا میده و نمیتونم خطاشو برطرف کنم ممنون میشم اگه کمکم کنید


126979




string strInput = strFields.Substring(0, strFields.Length - 1);
char[] commaSeparator = new char[] { ',' };
string[] filds = strInput.Split(commaSeparator);
dt1.Clear();
object[] list = new object[dt1.Columns.Count];
int j = 0;
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
j = 0;
foreach (string fild in filds)
{
if (fild == "CodeSH")
list[j] = dataGridView1.Rows[i].Cells[0].Value;
if (fild == "Area")
list[j] = dataGridView1.Rows[i].Cells[1].Value;
if (fild == "Jbo")
list[j] = dataGridView1.Rows[i].Cells[4].Value;
if (fild == "Sensor")
list[j] = dataGridView1.Rows[i].Cells[2].Value;
if (fild == "System")
list[j] = dataGridView1.Rows[i].Cells[5].Value;
if (fild == "Command")
list[j] = dataGridView1.Rows[i].Cells[3].Value;
if (fild == "Factory")
list[j] = dataGridView1.Rows[i].Cells[22].Value;
if (fild == "CodeLO")
list[j] = dataGridView1.Rows[i].Cells[6].Value;
if (fild == "InstallLO")
list[j] = dataGridView1.Rows[i].Cells[20].Value;
if (fild == "Segment")
list[j] = dataGridView1.Rows[i].Cells[7].Value;
if (fild == "NourishVOL")
list[j] = dataGridView1.Rows[i].Cells[8].Value;
if (fild == "MaintenanceVOL")
list[j] = dataGridView1.Rows[i].Cells[9].Value;
if (fild == "StreamNYMY")
list[j] = dataGridView1.Rows[i].Cells[10].Value;
if (fild == "PeakStream")
list[j] = dataGridView1.Rows[i].Cells[11].Value;
if (fild == "DisposalPower")
list[j] = dataGridView1.Rows[i].Cells[12].Value;
if (fild == "X")
list[j] = dataGridView1.Rows[i].Cells[13].Value;
if (fild == "Y")
list[j] = dataGridView1.Rows[i].Cells[14].Value;
if (fild == "Z")
list[j] = dataGridView1.Rows[i].Cells[15].Value;
if (fild == "Ip")
list[j] = dataGridView1.Rows[i].Cells[16].Value;
if (fild == "Scale")
list[j] = dataGridView1.Rows[i].Cells[17].Value;
if (fild == "NumBoard")
list[j] = dataGridView1.Rows[i].Cells[18].Value;
if (fild == "Install")
list[j] = dataGridView1.Rows[i].Cells[19].Value;
if (fild == "Outside")
list[j] = dataGridView1.Rows[i].Cells[21].Value;
if (fild == "Description")
list[j] = dataGridView1.Rows[i].Cells[23].Value;
j = j + 1;
}
dt1.Rows.Add(list);


توضیحات کد: تو یه صفحه به کاربر این امکان رو میدم که ستونهای دلخواهش رو واسه نمایش در گزارش انتخاب کنه این ستونها رو بصورت یه آرایه رشته ای دریافت می کنم بعد نام هر ستون رو به عنوان یه زیر رشته جدا می کنم و به ازای هر نام ستون وارد شده اون ستون رو از داده های دیتاگریدویو جدا کرده به یه لیست اضافه میکنم که بعدا اون لیست رو به دیتاتیبلی که قرار هست به گزارش ارسال شه add میکنم.
مشکلی که دارم اینه که فرضا اگه ستون Area توسط کاربر انتخاب شده باشه کد زیر باید اجرا شه



if (fild == "Area")
list[j] = dataGridView1.Rows[i].Cells[1].Value;

اما بلافاصله بعد از اجرای خط دوم این خطا که تصویرشو گذاشتم رخ میده.علتش چی هست؟!!!

parvizwpf
یک شنبه 07 دی 1393, 15:12 عصر
این ارور داره میگه شما اندیس فلانم رو ندارید ببنید i در اون لحظه چنده و آیا واقعا وجود داره همچین سطری؟

adineh67
یک شنبه 07 دی 1393, 15:55 عصر
چک کردم i مقدار صفر میگیره و اولین سطرو برمیگردونه ایراد از i نیست

parvizwpf
یک شنبه 07 دی 1393, 16:26 عصر
j چطور؟ خلاصه یکی از اون اندیسها از اندازه واقعی خارجه