2017-06-21 85 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,它工作得很好。 –

相關問題