-2
我得到这个错误:类型 'System.ArgumentOutOfRangeException' 未处理的异常在mscorlib.dll中发生的datagridview
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
这里是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace karamouzi
{
public partial class importexcel : Form
{
db db1 = new db();
public importexcel()
{
InitializeComponent();
}
private void importexcel_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Excel file(.xls)|*.xls";
openFileDialog1.FilterIndex = 1;
bool userClickedOK =Convert.ToBoolean(openFileDialog1.ShowDialog());
if (userClickedOK == true)
{
url.Text = openFileDialog1.FileName;
dataGridView1.DataSource = db1.GetDataFromExcel(url.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
for (int counter = 1; counter < dataGridView1.Rows.Count; counter++)
{
//if (dataGridView1.Rows[counter].Cells[1].Value != null)
//{
// if (dataGridView1.Rows[counter].Cells[1].Value.ToString().Length != 0)
//{
error Line
db1.command("insert into tblStInfo(xStCode,xFname,xLname,xMajor,xStTell,xMeliCode) values('" + dataGridView1.Rows[counter].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[counter].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[counter].Cells[3].Value.ToString() + "','" + dataGridView1.Rows[counter].Cells[4].Value.ToString() +"','"+dataGridView1.Rows[counter].Cells[5].Value.ToString()+"','"+dataGridView1.Rows[counter].Cells[6].Value.ToString() +"')");
//}
//}
}
MessageBox.Show("اطلاعات با موفقیت ثبت شد");
}
private void btnExite_Click(object sender, EventArgs e)
{
Close();
}
}
}
这只是一个错误和一些代码。你需要解释更多关于你的问题。就像在哪一行发生错误一样。 –
(该解决方案出现在错误消息中)。打开异常停止在发生错误的行。欢迎来到Stackoverflow :) – Grantly