简体   繁体   English

如何动态增加Datagridview的行并立即获得组合框的值?

[英]How to increase rows of Datagridview dynamically and get the combobox values all at once?

I have seen many question relevant to that here but none of them make any sense to me. 我在这里看到了许多与此有关的问题,但对我来说,这些问题都没有。 So anyone who could help me out here. 所以任何可以在这里帮助我的人。 First of all i am scraping data from Amazon site and saving the data in this DataGridView 首先,我从亚马逊网站上抓取数据并将数据保存在此DataGridView中

dataGridViewScraping Data: dataGridViewScraping数据:

在此处输入图片说明

dataGridViewASINs: dataGridViewASINs:

在此处输入图片说明

i successfully scrape first page data but when i try to scrape 2nd data and try to put the data in the datagridview i get the error 我成功地抓取了第一页数据,但是当我尝试抓取第二个数据并尝试将数据放入datagridview时,出现错误

index out of range. 索引超出范围。 Must be non negative and ** 必须为非负且**

I am also getting an error here when the loop comes back for the 2nd time and first data which i put into my DataGridView is title: 当循环第二次返回并且我放入DataGridView的第一个数据是title时,我在这里也遇到错误:

 for (int i = 0; i < dataGridViewASINs.Rows.Count - 1; i++)
    {
 //Getting Title
            string title = driver.FindElement(By.Id("productTitle")).GetAttribute("innerText");
            dataGridViewScrapingData.Rows[i].Cells[cols].Value = title;
}

I am using this code for putting the data in the datagridview all the other columns code is similar to that i am using 我正在使用此代码将数据放入datagridview中的所有其他列代码类似于我正在使用的代码

Rows[index].Cells[Indexing] 行[指数] .Cells [索引]

for all the columns but for Combobox columns i didn't use this indexing i think so that also works only for first iteration 对于所有列,但对于组合框列,我认为我没有使用此索引,因此也仅适用于第一次迭代

 for (int i = 0; i < dataGridViewASINs.Rows.Count - 1; i++)
        {
 List<IWebElement> imageCounts = driver.FindElements(By.XPath("//ul[@class='a-unordered-list a-nostyle a-button-list a-vertical a-spacing-top-extra-large']//li[@class='a-spacing-small item imageThumbnail a-declarative']//span[@class='a-button-text']//img")).ToList();        
                element = driver.FindElement(By.Id("landingImage"));
                comboState.Items.Add(element.GetAttribute("src"));        
                for (int j = 0; j < imageCounts.Count - 1; j++)
                {
                    //Clicking that Element
                    string GenricXpath = "//ul[@class='a-unordered-list a-nostyle a-button-list a-vertical a-spacing-top-extra-large']//li[" + (j + 5).ToString() + "]//span[1]//span[1]//span[1]//input[1]";
                    element = driver.FindElement(By.XPath(GenricXpath)); element.Click();
                    //Extracting URL now
                    string AnotherXpath = "//li[@class='image item itemNo" + (j + 1).ToString() + " maintain-height selected']//img";
                    element = driver.FindElement(By.XPath(AnotherXpath)); comboState.Items.Add(element.GetAttribute("src"));
                }
                dataGridViewScrapingData.Columns.Add(comboState);
}

Other than that i also wanna know after putting data into datagridviewScrapingData. 除此之外,我还想将数据放入datagridviewScrapingData之后。 I don't know how i can get back the entire data which is in the combobox column in the DataGridViewScraping Data. 我不知道如何获取DataGridViewScraping数据的combobox列中的全部数据。 I want to get the data into the List of string from the datagridviewScrapingData where i have saved my entire data. 我想将数据从datagridviewScrapingData获取到字符串列表中,在此我已保存了全部数据。 I have seen many questions relevant to that as well here on stackoverflow but none of them make any sense to me. 我在stackoverflow上也看到了许多与此相关的问题,但是对我来说,这些问题都没有任何意义。

Looks like your approach of working with dataGridView is a bit incorrect. 看来您使用dataGridView的方法有点不正确。 You try to work with the data grid rows directly, but the idea is to have a separate collection like List<Product> and display it through a binding source. 您尝试直接使用数据网格行,但其想法是拥有一个单独的集合,如List<Product>并通过绑定源显示它。

1. First create a class representing your product like: 1.首先创建一个代表您的产品的类,例如:

public class Product
{
    public string Title { get; set; }
    public string Asin { get; set; }
}

2. Create a list of products and scrape them to that list. 2.创建一个产品列表并将其刮到该列表。

3. Click on the dataGridView in form designer and note the arrow button at the top right corner. 3.在表单设计器中单击dataGridView,然后注意右上角的箭头按钮。 Click on it and generate a new datasource by selecting your Product class. 单击它,然后通过选择您的Product类来生成新的数据源。 DataGridBindingSource will appear at the bottom of the form designer. DataGridBindingSource将出现在表单设计器的底部。 Let's assume it's name is dataGridViewBindingSource . 假设它的名称是dataGridViewBindingSource

4. Assign your products collection to the binding source: 4.将您的产品集合分配给绑定源:

dataGridViewBindingSource.DataSource = products;

Now you can modify products collection and display updated products in the grid by calling dataGridView.Refresh() method. 现在,您可以通过调用dataGridView.Refresh()方法来修改产品集合并在网格中显示更新的产品。 At this point you should get rid of "Index out of range" exception and you have a reference to your products collection, so you don't have to "extract" them explicitly from datagrid rows. 此时,您应该摆脱“索引超出范围”异常,并且可以引用产品集合,因此不必从数据网格行中显式“提取”它们。

5. Instead of getting values from ComboBox you can store options in a product first and then add them to a combobox. 5.无需从ComboBox中获取值,您可以先将选项存储在产品中,然后将其添加到组合框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM