2014-12-01 63 views
0

我已经从excel中的列表向我的datagridview添加了列标题。现在我有一个字典,其中列标题和值需要插入到新行中。每行一个字典。基于列标题值在数据网格视图中插入数据C#

foreach (DataGridViewRow row in dgv_ExitPoints.Rows) 
{ 
    for (int j = 0; j < dgv_ExitPoints.Columns.Count; j++) 
    { 
     String header = dgv_ExitPoints.Columns[j].HeaderText; 
      if(exitPointDictionary.ContainsKey(header) 
      { 
       ??? 
      } 
    } 
} 
+0

一旦知道密钥存在,可以通过'exitPointDictionary [header]'访问该值并将其添加到行中。 – stuartd 2014-12-01 21:04:18

+0

这里从技术上讲没有问题。 – Anthony 2014-12-01 21:06:25

+0

谢谢,但我如何插入行?有没有更有效的方法呢? – user1875342 2014-12-01 21:06:30

回答

0

我解决了它。如果有人对未来感兴趣:

`for (int j = 0; j < dgv_ExitPoints.Columns.Count; j++) 
{ 
    String header = dgv_ExitPoints.Columns[j].HeaderText; 
    if(exitPointDictionary.ContainsKey(header)) 
    { 
     dgv_ExitPoints.Rows[counter].Cells[j].Value = exitPointDictionary[header].ToString(); 
    } 
    ++counter; 
}`