Haris Hussain - Posted on Monday, June 30, 2008 23:15:15

I am facing a following exception. While am deleting a row from gridview. I am using VS 2005.

Exception: Index was out of range.Parameter must be non negative and less than the index collection. Parameter Name : INDEX

private void bindingNavigatorDeleteItem_Click()
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(@"Data Source=HARIS\SQLEXPRESS;Initial Catalog=Nesk;Integrated Security=True");
da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_Nsk_PromoCriteriaDelete");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@promoID", SqlDbType.VarChar).Value = System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0].Cells[0].Value);
cmd.Connection = conn;
da.SelectCommand = cmd;
catch(Exception ex)

Stored Procedure:
ALTER PROCEDURE [dbo].[sp_Nsk_PromoCriteriaDelete]
@PromoID varchar(20)
delete from Nsk_PromoCriteria where PromoId = @PromoID

Property Set:

RowTemplate: DataGridViewRow Index{-1

Recommended Answers

All 4 Replies

What line throws your exception?

What line throws your exception?


throws an exception.

SelectedRows[0] throws an exception while i select last record. There are six rows in my table.
when i select last row it throws an exception while i select upper 5 rows it selects the very next row that i selected.

when i use selectedrows[0].count it gives value 5. While there is six rows in my tables.

I checked MSDN page for selectedrows property. it says

The SelectionMode property must be set to FullRowSelect or RowHeaderSelect for the SelectedRows property to be populated with selected rows.

just check SelectionMode property on properties window and make sure it is set to fullrow select. and make sure that it is not empty before you check. I'm sure there is an isEmpty or length function in the DataGridView.

Do you tried this .,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.