2012-03-25 39 views
2

我有一个代码如下:系统参数超出范围的异常,同时增加行datagridview的

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

什么是堆栈跟踪? – SLaks 2012-03-25 23:09:39

+0

我不确定如何查看,但只要程序读取此行即可.Cells [“Local_Variables”]。Value =“test”;它给出了错误。 – 2012-03-25 23:14:49

+1

查看详细信息... – SLaks 2012-03-25 23:15:12

回答

2

Cell s“Local_Variables”和“System_Variables”在row.Cells中不存在。确保你先添加它们。

dataGridView1.Columns.Add("Local_Variables", "Local Variables"); 
dataGridView1.Columns.Add("System_Variables", "Local Variables"); 

如果列已经存在,然后检查他们的Key值,以确保他们正确设置。

+0

它给了我错误“没有超载的方法'添加'需要'2'参数 – 2012-03-25 23:21:10

+0

@Csharp_learner哎呀,我搞砸了代码 – Lander 2012-03-25 23:23:10

+0

我再次收到一个错误不包含定义列。 – 2012-03-25 23:30:21