代码如下:             
        int index = this.UserDgv.SelectedRows[0].Index;
                //int index = UserDgv.SelectedRows[0].Index;
                //初始化编辑通讯录的窗体并打开
                UpdateAddForm newform = new UpdateAddForm();
                //向编辑通讯录的窗体传递参数
                newform.UserIDStr = UserDgv.Rows[index].Cells["UserID"].Value.ToString();
                newform.UserNameStr = UserDgv.Rows[index].Cells["UserName"].Value.ToString();
                newform.UserSexStr = UserDgv.Rows[index].Cells["UserSex"].Value.ToString();
                newform.UserAreaStr = UserDgv.Rows[index].Cells["UserArea"].Value.ToString();
                newform.UserEmailStr = UserDgv.Rows[index].Cells["UserEmail"].Value.ToString();
                //打开新窗体
                newform.ShowDialog();
                FillUserInfor();

解决方案 »

  1.   

    错误提示: 必须为非负值并小于集合大小
    参数名:index我测试过编辑其他行的时候都没有报错,只有第一行的时候出错了
      

  2.   

    System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
    参数名: index
       在 System.Collections.ArrayList.get_Item(Int32 index)
       在 System.Windows.Forms.DataGridViewSelectedRowCollection.get_Item(Int32 index)
       在 SendMail.AddManageForm.button2_Click(Object sender, EventArgs e) 位置 E:\SendMail\SendMail\SendMail\AddManageForm.cs:行号 50
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      

  3.   

    把SelectedRows[0].Index替换为:CurrentRow.Index
      

  4.   

    int index = this.UserDgv.SelectedRows[0].Index;
    这句话看不懂。
      

  5.   

    int index = this.UserDgv.SelectedRows[0].Index; 
    你每次取的都是第一行啊,没有变啊
    int index = this.UserDgv.SelectedRows[CurrentRow.Index].Index; 
    还差不多吧,index是一个变的数字吧
      

  6.   

    不得与 我这样搞了...
                try
                {                //获得修改行的索引
                    int index = this.UserDgv.SelectedRows[0].Index;
                    //int index = UserDgv.SelectedRows[0].Index;
                    //初始化编辑通讯录的窗体并打开
                    UpdateAddForm newform = new UpdateAddForm();
                    //向编辑通讯录的窗体传递参数
                    newform.UserIDStr = UserDgv.Rows[index].Cells["UserID"].Value.ToString();
                    newform.UserNameStr = UserDgv.Rows[index].Cells["UserName"].Value.ToString();
                    newform.UserSexStr = UserDgv.Rows[index].Cells["UserSex"].Value.ToString();
                    newform.UserAreaStr = UserDgv.Rows[index].Cells["UserArea"].Value.ToString();
                    newform.UserEmailStr = UserDgv.Rows[index].Cells["UserEmail"].Value.ToString();
                    //打开新窗体
                    newform.ShowDialog();
                    FillUserInfor();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    UpdateAddForm newform = new UpdateAddForm();
                    //向编辑通讯录的窗体传递参数
                    newform.UserIDStr = UserDgv.Rows[0].Cells["UserID"].Value.ToString();
                    newform.UserNameStr = UserDgv.Rows[0].Cells["UserName"].Value.ToString();
                    newform.UserSexStr = UserDgv.Rows[0].Cells["UserSex"].Value.ToString();
                    newform.UserAreaStr = UserDgv.Rows[0].Cells["UserArea"].Value.ToString();
                    newform.UserEmailStr = UserDgv.Rows[0].Cells["UserEmail"].Value.ToString();
                    //打开新窗体
                    newform.ShowDialog();
                    FillUserInfor();
                }
      

  7.   

    int index = this.UserDgv.SelectedRows[0].Index;  
    你每次取的都是第一行啊,没有变啊 
    他这是取了么?他没选哪来的值?而且这种写法有么SelectedRows[0].
    你try catch有什么用?根本就不符合自己的要求。
    try catch只是要捕捉不可预知的错误,你try什么啊。现在知道有错误,要找出来。
      

  8.   

    UserDgv行为0的时候就会报这个错,判断下