为什么我得到的错误就像我必须非负?

时间:2021-06-13 11:21:39

I got Must be non negative,index was out of range error though there is data in all cells and the index is not overflowed too

我得到必须非负,索引超出范围错误虽然所有单元格中都有数据且索引也没有溢出

private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmProfileMaster frm = new frmProfileMaster();
            frm.status = "Edit";
            frm.pid = Convert.ToInt32(gdvProfile.SelectedRows[0].Cells[0].Value);
            frm.Show();
        }

2 个解决方案

#1


1  

It seems that there is no data available, or no row present at 0th index.

似乎没有数据可用,或者在第0个索引处没有行。

Hence, when you are pointing to SelectedRows[0].Cells[0] , since it is having no row, it gives you the error.

因此,当您指向SelectedRows [0] .Cells [0]时,由于它没有行,它会给您错误。

In quickwatch see the values you are getting and accordingly make the code.

在quickwatch中查看您获得的值并相应地制作代码。

#2


0  

Here you want is your gridviews first cell controls value. Right. You can try like,

这里你想要的是你的gridviews第一个单元格控件值。对。你可以试试,

 //if your first cell control is label then
 Label lbl = (Label)gdvProfile.Rows[0].FindControl("lbl");
 frm.pid = Convert.ToInt32(lbl.Text);

 Same way you can check for all control.

Hope it helps.

#1


1  

It seems that there is no data available, or no row present at 0th index.

似乎没有数据可用,或者在第0个索引处没有行。

Hence, when you are pointing to SelectedRows[0].Cells[0] , since it is having no row, it gives you the error.

因此,当您指向SelectedRows [0] .Cells [0]时,由于它没有行,它会给您错误。

In quickwatch see the values you are getting and accordingly make the code.

在quickwatch中查看您获得的值并相应地制作代码。

#2


0  

Here you want is your gridviews first cell controls value. Right. You can try like,

这里你想要的是你的gridviews第一个单元格控件值。对。你可以试试,

 //if your first cell control is label then
 Label lbl = (Label)gdvProfile.Rows[0].FindControl("lbl");
 frm.pid = Convert.ToInt32(lbl.Text);

 Same way you can check for all control.

Hope it helps.