简体   繁体   English

单击dataGridView时出错

[英]Error when clicked on dataGridView

When i click on dataGridView it throws an error saying "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll 当我点击dataGridView时,它会抛出一个错误,指出“mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的未处理异常

Additional information: Index was out of range. 附加信息:指数超出范围。 Must be non-negative and less than the size of the collection." 必须是非负的且小于集合的大小。“

Here is code section, i need it to, when clicked on some row, to write all parameters in textBoxes so it can be easily updated 这是代码部分,我需要它,当点击某一行时,写入textBoxes中的所有参数,以便它可以很容易地更新

 private void dgvList_MouseClick(object sender, MouseEventArgs e)
        {
                tbName.Text = dgvList.SelectedRows[0].Cells[0].Value.ToString();
                tbSurname.Text = dgvList.SelectedRows[1].Cells[1].Value.ToString();
                tbMobile.Text = dgvList.SelectedRows[2].Cells[2].Value.ToString();
                tbEmail.Text = dgvList.SelectedRows[3].Cells[3].Value.ToString();
                cbCategory.Text = dgvList.SelectedRows[4].Cells[4].Value.ToString();
        }

You are getting that error cause either your gridview don't have 5 rows or 5 columns. 您收到该错误导致您的gridview没有5行或5列。 How many rows/columns does your gridview has? 您的gridview有多少行/列? Remember that array indexing starts with 0 . 请记住,数组索引从0开始。 Make sure, the row number (or) column number you are accessing does exists. 确保您访问的行号(或)列号确实存在。

Moreover, why are you accessing multiple rows at same time like below. 此外,为什么你要同时访问多行,如下所示。 That don't make sense cause at any time only 1 row will be selected. 这没有意义,因为任何时候只会选择1行。 ... Right? ... 对?

dgvList.SelectedRows[0]
dgvList.SelectedRows[1]

It rather should be probably be 它应该是可能的

 private void dgvList_MouseClick(object sender, MouseEventArgs e)
        {
                tbName.Text = dgvList.SelectedRows[0].Cells[0].Value.ToString();
                tbSurname.Text = dgvList.SelectedRows[0].Cells[1].Value.ToString();
                tbMobile.Text = dgvList.SelectedRows[0].Cells[2].Value.ToString();
                tbEmail.Text = dgvList.SelectedRows[0].Cells[3].Value.ToString();
                cbCategory.Text = dgvList.SelectedRows[0].Cells[4].Value.ToString();
        }

Thanks @stackuser83 for pointing that. 感谢@ stackuser83指出这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM