我的代码如下:
private void svars_MouseDoubleClick(object sender, MouseEventArgs e)
{
userSelection user_sel = new userSelection();
string file = svars.SelectedItem.ToString();
DataGridViewRow row = new DataGridViewRow();
dataGridView1.Rows.Add(row);
row.Cells["Local_Variables"].Value = "test";
DataGridViewCell sysvar = new DataGridViewTextBoxCell();
sysvar.Value = file;
row.Cells["System_Variables"] = sysvar;
}
我收到错误后出现错误:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: Specified argument was out of the range of valid values.
当我尝试调试时,我发现row.Index是-1。我不知道如何解决它。任何人都可以帮忙。
感谢。
答案 0 :(得分:2)
Cell
中不存在row.Cells
s“Local_Variables”和“System_Variables”。确保先添加它们。
dataGridView1.Columns.Add("Local_Variables", "Local Variables");
dataGridView1.Columns.Add("System_Variables", "Local Variables");
如果列已经存在,请检查其Key
值以确保它们设置正确。