locked
Index was out of range. Must be non-negative and less than the size of the collection RRS feed

  • Question

  • Following is my code snippet to remove selected row from the datagridview.

    I don't know, where m i doing wrong, getting an error  : "Index was out of range. Must be non-negative and less than the size of the collection"

    private void button1_Click(object sender, EventArgs e)
            {
                if (cnn.State == ConnectionState.Open )
                {
                    cnn.Close();
                }
                else
                {
                    cnn.Open();
                    string sql = "Select * from Login_Mast";
                    SqlCommand scommand = new SqlCommand(sql, cnn);
                    SqlDataAdapter sAdapter = new SqlDataAdapter(scommand);
                    SqlCommandBuilder sBuilder = new SqlCommandBuilder(sAdapter);
                    DataSet sDS = new DataSet();
                    DataTable sTable;
                    sAdapter.Fill(sDS, "Login_Mast");
                    sTable = sDS.Tables["Login_Maast"];
                    cnn.Close();
                    dataGridView1.DataSource = sDS.Tables["Login_Mast"];
                    dataGridView1.ReadOnly = true;
                    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
     
                    if(MessageBox.Show ("Do you want to delete this record ?", "Delete", MessageBoxButtons.YesNo)==DialogResult.Yes )
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                        sAdapter.Update(sTable);
                    }
                   
                    MessageBox.Show ("Record deleted successfully");

     
                      
                    }
    }

    Monday, December 1, 2014 10:41 AM

Answers

  • You are trying to delete the selected row of the DataGridView in a button click event.

    But you are binding the DataGridView every time when the button is clicked. Therefore the selected row(s) will be lost and will always be empty. The dataGridView1.SelectedRows collection will not have any element in it. It means that you can't ever delete the selected row.

    Hence dataGridView1.SelectedRows(0) throws exception. You need to put the DataGridView binding code in a different event such as Form's load as follows:

     private void Form1_Load(object sender, EventArgs e)
            {
          if (cnn.State == ConnectionState.Open )
                {
                    cnn.Close();
                }
                else
                {
                    cnn.Open();
                    string sql = "Select * from Login_Mast";
                    SqlCommand scommand = new SqlCommand(sql, cnn);
                    SqlDataAdapter sAdapter = new SqlDataAdapter(scommand);
                    SqlCommandBuilder sBuilder = new SqlCommandBuilder(sAdapter);
                    DataSet sDS = new DataSet();
                    DataTable sTable;
                    sAdapter.Fill(sDS, "Login_Mast");
                    sTable = sDS.Tables["Login_Maast"];
                    cnn.Close();
                    dataGridView1.DataSource = sDS.Tables["Login_Mast"];
                    dataGridView1.ReadOnly = true;
                    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
       }

    The button click event should only have code to delete the selected row as follows:

    private void button1_Click(object sender, EventArgs e)
            {
         if(MessageBox.Show ("Do you want to delete this record ?", "Delete", MessageBoxButtons.YesNo)==DialogResult.Yes )
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                        sAdapter.Update(sTable);
                    }               
                    MessageBox.Show ("Record deleted successfully");
    
          }

    Also there is a mistake in the table name 'Login_Mast' in the following line

          sAdapter.Fill(sDS, "Login_Mast");
          Table = sDS.Tables["Login_Maast"]; ---> This should be Table = sDS.Tables["Login_Mast"];








    Microsoft MVP - ASP/ASP.NET

    • Proposed as answer by Kristin Xie Tuesday, December 2, 2014 9:55 AM
    • Marked as answer by Kristin Xie Monday, December 8, 2014 3:16 AM
    Monday, December 1, 2014 12:17 PM

All replies

  • In what line does your problem occurs?

    This happens when you try to access and index of an array (collection) that does not exists.

    Check the line below if dataGridView1.SelectedRows[0].Index is a valid index on dataGridView1.Rows before trying to use RemoveAt.

            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);




    Monday, December 1, 2014 11:15 AM
  • Firstly you should check whether any of the rows are selected on your datagridview:

    if(dataGridView1.SelectedRows.Count > 0)
    {               dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                        sAdapter.Update(sTable);
    }

    Monday, December 1, 2014 11:25 AM
  • You are trying to delete the selected row of the DataGridView in a button click event.

    But you are binding the DataGridView every time when the button is clicked. Therefore the selected row(s) will be lost and will always be empty. The dataGridView1.SelectedRows collection will not have any element in it. It means that you can't ever delete the selected row.

    Hence dataGridView1.SelectedRows(0) throws exception. You need to put the DataGridView binding code in a different event such as Form's load as follows:

     private void Form1_Load(object sender, EventArgs e)
            {
          if (cnn.State == ConnectionState.Open )
                {
                    cnn.Close();
                }
                else
                {
                    cnn.Open();
                    string sql = "Select * from Login_Mast";
                    SqlCommand scommand = new SqlCommand(sql, cnn);
                    SqlDataAdapter sAdapter = new SqlDataAdapter(scommand);
                    SqlCommandBuilder sBuilder = new SqlCommandBuilder(sAdapter);
                    DataSet sDS = new DataSet();
                    DataTable sTable;
                    sAdapter.Fill(sDS, "Login_Mast");
                    sTable = sDS.Tables["Login_Maast"];
                    cnn.Close();
                    dataGridView1.DataSource = sDS.Tables["Login_Mast"];
                    dataGridView1.ReadOnly = true;
                    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
       }

    The button click event should only have code to delete the selected row as follows:

    private void button1_Click(object sender, EventArgs e)
            {
         if(MessageBox.Show ("Do you want to delete this record ?", "Delete", MessageBoxButtons.YesNo)==DialogResult.Yes )
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                        sAdapter.Update(sTable);
                    }               
                    MessageBox.Show ("Record deleted successfully");
    
          }

    Also there is a mistake in the table name 'Login_Mast' in the following line

          sAdapter.Fill(sDS, "Login_Mast");
          Table = sDS.Tables["Login_Maast"]; ---> This should be Table = sDS.Tables["Login_Mast"];








    Microsoft MVP - ASP/ASP.NET

    • Proposed as answer by Kristin Xie Tuesday, December 2, 2014 9:55 AM
    • Marked as answer by Kristin Xie Monday, December 8, 2014 3:16 AM
    Monday, December 1, 2014 12:17 PM