【问题标题】:ArgumentOutOfRangeException - index was out of range. Must be non-negative and less than the size of the collectionArgumentOutOfRangeException - 索引超出范围。必须是非负数且小于集合的大小
【发布时间】:2018-07-10 09:25:30
【问题描述】:

错误: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)
        {

        }
    }
}

我该如何克服这个错误?

【问题讨论】:

  • 你有堆栈跟踪吗? ...如果我猜...您的 dgvUser 没有任何列...所以 dgvUser.Columns[0] 想要访问一组空列的第一列...
  • 是的,是的,但没有解决
  • 你初始化dgvUser.Columns了吗?
  • 请不要在标题和代码列表中转储错误而不说在哪里发生错误
  • Display()开头设置断点并检查dgvUser.Columns.Count属性的值

标签: c# datagridview datatable


【解决方案1】:

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

【讨论】:

  • @miladhosayni 将列添加到您的 DataGridView
  • 你需要能够单独调试你的代码,在你的代码中设置一个断点,看看做什么/不做什么以及为什么..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多