ArgumentOutOfRangeException-索引超出范围。必须为非负数并且小于集合的大小

时间:2018-07-10 09:25:30

标签: c# datagridview datatable

错误:ArgumentOutOfRangeException-索引超出范围。必须为非负数并且小于集合的大小

方案:一个桌面应用程序,它从SQL Server数据库读取数据。当我想单击按钮时,它将打开表单并从数据库中读取数据,然后将其自动放入“数据网格视图”中,但这会出现异常。我将代码放下

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;
using System.Data.SqlClient;

namespace BashgaheVarzeshiI2
{
    public partial class frmuser : Form
    {
        public frmuser()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection("Data source=(local);initial catalog=BashgahDB; integrated security = true");
        SqlCommand cmd = new SqlCommand();

        void Display()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from Karbar";
            adp.Fill(ds, "Karbar");
            dgvUser.DataSource = ds;
            dgvUser.DataMember = "Karbar";

            dgvUser.Columns[0].HeaderText = "code";
            dgvUser.Columns[1].HeaderText = "username";
            dgvUser.Columns[2].HeaderText = "password";

            dgvUser.Columns[0].Width = 50;
        }
        private void bunifuImageButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bunifuImageButton2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void bunifuImageButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bunifuImageButton3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "insert into Karbar(UName,Password)values(@Uname,@Password)";
                cmd.Parameters.AddWithValue("@Uname", bunifuMaterialTextbox1.Text);
                cmd.Parameters.AddWithValue("@Password", bunifuMaterialTextbox2.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                Display();
                MessageBox.Show("Username Saved");
            }
            catch (Exception)
            {
                MessageBox.Show("Error!");
            }
        }

        private void frmuser_Load(object sender, EventArgs e)
        {
            Display();
        }

        private void bunifuImageButton4_Click(object sender, EventArgs e)
        {
            try
            {
                int x = Convert.ToInt32(dgvUser.SelectedCells[0].Value);

                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "Delete From Karbar where id=@N";
                cmd.Parameters.AddWithValue("@N", x);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                Display();
                MessageBox.Show("User Deleted!");
            }
            catch (Exception)
            {
                MessageBox.Show("Error!");
            }
        }

        private void dgvUser_SelectionChanged(object sender, EventArgs e)
        {

        }
    }
}

如何克服此错误?

1 个答案:

答案 0 :(得分:0)

您正在尝试访问不存在且在范围之外的数组元素。查看您的代码,在Display上调用了Load方法,因此看来Columns索引是问题所在,即您在尝试访问它们之前未设置任何列。