指数超出范围。必须是非负数且小于集合的大小。参数名称:index1

时间:2014-03-22 08:21:26

标签: datagridview

 int n = Convert.ToInt32(GridProduct.SelectedRows[0].Cells[0].Value);
int ill = GrdSubProduct.Rows.Count;

            if (GridProduct.Rows.Count > 1 && GridProduct.SelectedRows[0].Index != GridProduct.Rows.Count - 1)
            {

                SqlConnection con = new SqlConnection(str);

                SqlCommand cmd = new SqlCommand("SP_Del_DeleteBySubProductID", con);
                cmd.Parameters.AddWithValue("@ID", GridProduct.SelectedRows[0].Cells[0].Value);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                int i = cmd.ExecuteNonQuery();

                if (i > 0)
                    MessageBox.Show(" Product Delete");

                GetProduct();

            }

帮助我............

1 个答案:

答案 0 :(得分:0)

哪一行导致异常?我的猜测是第一行,因为如果你没有将SelectionMode设置为FullRowSelect,那么GridProduct.SelectedRows为null,这不是默认值......

MSDN:

  

SelectionMode属性必须设置为FullRowSelect或RowHeaderSelect,以便使用选定的行填充SelectedRows属性。