简体   繁体   中英

exception when assigning currentcell datagridview

I have a very strange exception. I have a datagridview bind to some data. And when the datasource is empty I add some data to it, update the datagridview and try to set the row I added to editmode. Here is the code:

foreach (DataGridViewRow row in dataGridViewActionTemplates.Rows)
{
    if (((GridActionTemplate)row.DataBoundItem).ID == actTemplate.Id)
    {
        dataGridViewActionTemplates.CurrentCell = row.Cells[dataGridViewActionTemplates.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Index];
        dataGridViewActionTemplates.BeginEdit(false);
        break;
    }
 }

When I try to perform an assignment I get an 'IndexOutOfRange' Exception and it says 'Index -1 does not have a value.' It is very strange, because index in the right part of assignment is bigger then zero.

I'll be very gratefull for help

I recall running into somewhat similar weirdness that I eventually discovered was related to the selected Windows theme in Windows 7. At the time my code worked normally on Aero with no exceptions but behaved similar to yours when the Classic theme was enabled in Win 7 or when the code ran on Windows 2003. It wasn't the exact same situation as yours, so I don't know if it's the same problem or not, but that might be one possible thing to look at. The specific issue was relating to the index of the column and row headers, which strangely do not return the same value in the 2 different themes. That said, it could be that you're unknowingly trying to modify the row header field instead of the first actual data field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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