简体   繁体   English

分配currentcell datagridview时发生异常

[英]exception when assigning currentcell datagridview

I have a very strange exception. 我有一个非常奇怪的例外。 I have a datagridview bind to some data. 我有一个datagridview绑定到一些数据。 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. 当数据源为空时,我向其中添加一些数据,更新datagridview并尝试设置我添加到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.' 当我尝试执行分配时,出现“ IndexOutOfRange”异常,并显示“ Index -1没有值”。 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. 我回想起最终发现与Windows 7中选定的Windows主题有关的某种奇怪现象。当时,我的代码在Aero上正常工作,没有例外,但在Win 7中启用Classic主题时或当该代码在Windows 2003上运行。情况与您的情况不完全相同,因此我不知道这是否是相同的问题,但这可能是一件事。 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. 就是说,这可能是您在不知不觉中试图修改行标题字段而不是第一个实际数据字段。

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

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