Click here to Skip to main content
15,896,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace testdb
{
    public partial class Form1 : Form
    {
        string constr = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:/Users/Xprts_3/Documents/Database1.accdb";
       

       
        public Form1()
        {
            InitializeComponent();
            Bind();
        }
        
        private void Bind()
        {

            OleDbConnection con = new OleDbConnection(constr);   
   con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("select * from tb1", con);
            DataTable dt = new DataTable();
           da.Fill(dt);
          dataGridView1.DataSource = dt;

          


       
            con.Close();
        }

        private void btnSave_Click_Click(object sender, EventArgs e)
        {

            OleDbConnection con = new OleDbConnection(constr);   
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Insert Into tb1(name) Values (@name)", con);
            cmd.Parameters.AddWithValue("name", txtproject_name.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Inserted sucessfully");
            Bind();
            // Clear();
       


        }
        // delete  class code starts here

        private void btnDelete_Click(object sender, EventArgs e)
        {

            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand delcmd = new OleDbCommand();
            if (dataGridView1.Rows.Count > 1 && i != dataGridView1.Rows.Count - 1)
            {
                delcmd.CommandText = "DELETE FROM tb1 WHERE ID=" + dataGridView1.SelectedRows[i].Cells[0].Value.ToString() + "";
                con.Open();
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
                con.Close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
                MessageBox.Show("Row Deleted");
            }

        }
    
    }
       
   
}



the problem is that when i am clicking on delete button named btnDelete then it is giving an error that is "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"...plz tell me the solution
Posted
Updated 9-Jan-14 21:08pm
v2

1 solution

I believe the line i = dataGridView1.SelectedCells[0].RowIndex; is throwing the exception.
instead try using
C#
int i = datagridView1.CurrentCell.RowIndex;


or you can use
dataGridView1.SelectedRows[0].Cells[0] instead of dataGridView1.SelectedRows[i].Cells[0]
 
Share this answer
 
v2
Comments
Member 10512838 10-Jan-14 2:45am    
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index...is is still showing this error at the line

delcmd.CommandText = "DELETE FROM tb1 WHERE ID=" + dataGridView1.SelectedRows[i].Cells[0].Value.ToString() + "";....what to do now??
Naz_Firdouse 10-Jan-14 2:49am    
use dataGridView1.SelectedRows[0].Cells[0] instead of dataGridView1.SelectedRows[i].Cells[0]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900