-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