c#中的ArgumentOutOfRange异常 [英] ArgumentOutOfRange Exception in c#

查看:62
本文介绍了c#中的ArgumentOutOfRange异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序来打印证书.我打开了一个csv文件并读取了datagridview列表中的文件.然后它打印了datagridview列表中的内容.无法正常工作.当我尝试打开一个csv文件时,它显示捕获到的ArgumentOutOfRange异常,索引超出范围.必须为非负数,小于集合的大小."
我该怎么办?请帮助我.......

我的代码是这样的:

i have a web application to print a certificate.i have opened a csv file and read the file in a datagridview list.then it prints the contents in the datagridview list.everything runs fine.bt the problem is tat some csv files are not working.when i tried to open a csv file it shows "caught ArgumentOutOfRange Exception,index was out of range.must be non-negative less than the size of the collection."
what should i do?pls help me.......

my code is this:

private void openFileDialogOpenCSV_FileOk(object sender, CancelEventArgs e)
    {
        if (openFileDialogOpenCSV.CheckFileExists)
        {
            objStudents = new students();
            objSelectedStudents = new students();
            try
            {
                CsvFileReader objCsvFileReader = new CsvFileReader(openFileDialogOpenCSV.OpenFile());
                CsvRow objCsvRows = new CsvRow();
                while (objCsvFileReader.ReadRow(objCsvRows))
                {
                    student objStudent = new student(true, objCsvRows[0], objCsvRows[1], objCsvRows[2], objCsvRows[3], objCsvRows[4], objCsvRows[5]);
                    objStudents.Add(objStudent);
                    objSelectedStudents.Add(objStudent);
                }
                objCsvFileReader.Dispose();
                objCsvFileReader.Close();
                dataGridViewList.Rows.Clear();
                if ((objStudents != null) && (objStudents.Count > 0))
                {
                    panelList.BringToFront();
                    updateGridView();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

推荐答案

调试!
您的代码中存在一些逻辑错误,或者您对输入文件有一个假设,但这并不总是正确的.没有代码,引发异常的语句以及生成错误的输入文件没人会知道您的问题是什么.
因此,调试!
Debug!
You have some logical error in your code, or you have an assumption about the input file, that is not always true. Without code, the statement raising the exception, and the input file generating the error nobody will have a clue, what your problem is.
So, bebug!


这篇关于c#中的ArgumentOutOfRange异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆