2017-06-21 140 views
0

我有我已经由元素组成3只列出了我想插入我的datagridview, enter image description here如何将数据添加到datagridview中的特定列?

但问题是,当我尝试将我的数据网格的数据接收这样的事情,enter image description here

所以要插入数据我使用了下面的代码:

 IList<string> ruas = new List<string>(); 
     foreach (var element in Gdriver.FindElements(By.ClassName("search-title"))) 
     { 
      //ruas.Add(element.Text); 
      table.Rows.Add(element.Text); 


     } 


     IList<string> codps = new List<string>(); 
     foreach(var Codpelement in Gdriver.FindElements(By.ClassName("cp"))) 
     { 

      table.Rows.Add("",Codpelement.Text); 

     } 

     IList<string> Distritos = new List<string>(); 
     foreach (var Distritoelement in Gdriver.FindElements(By.ClassName("local"))) 
     { 


      //Distritos.Add(Distritoelement.Text); 
      table.Rows.Add("","",Distritoelement.Text.Substring(Distritoelement.Text.LastIndexOf(',') + 1)); 

     } 

请问您好,告诉我一个更好的方法,使数据从上到下出现?

谢谢。

+0

更好地创建一个数据表,然后使数据表成为DGV的数据源。 – jdweng

回答

0

问题是您每行输入一个值。你应该总共有三行,但你有3 * numberofcolumns行。我推荐使用以下方法:

IList<string> ruas = new List<string>(); 
    foreach (var element in Gdriver.FindElements(By.ClassName("search-title"))) 
    { 
     ruas.Add(element.Text); 
    } 


    IList<string> codps = new List<string>(); 
    foreach(var Codpelement in Gdriver.FindElements(By.ClassName("cp"))) 
    { 
     codps.Add(Codpelement.Text); 
    } 

    IList<string> Distritos = new List<string>(); 
    foreach (var Distritoelement in Gdriver.FindElements(By.ClassName("local"))) 
    { 
     Distritos.Add(Distritoelement.Text.Substring(Distritoelement.Text.LastIndexOf(',') + 1)); 
    } 

    for (int i = 0; i < ruas.Count; i++) { 
     table.Rows.Add(ruas.ElementAt(i), codps.ElementAt(i), Distritos.ElementAt(i)); 
    } 
+0

感谢您的支持!你能告诉我,为什么在栏目“Distritos”的第一行没有显示任何数据? –

+0

@PedroAlvim欢迎您。如果不知道Distritos的实际数据,很难说清楚。 Rua do Monte的Distrito可能以逗号和一些后面的空格结尾,因为在你的算法中,我们在最后一个逗号之后得到Distrito的一部分,参见:Distritoelement.Text.Substring(Distritoelement.Text.LastIndexOf( ',')+ 1) –

+0

我认为selenium与className选择器有问题,因为我将它更改为XPath,它工作得很好。 –

相关问题