简体   繁体   中英

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

dataGridViewScraping Data:

在此处输入图片说明

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

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:

 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

Rows[index].Cells[Indexing]

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. I don't know how i can get back the entire data which is in the combobox column in the DataGridViewScraping Data. I want to get the data into the List of string from the datagridviewScrapingData where i have saved my entire data. I have seen many questions relevant to that as well here on stackoverflow but none of them make any sense to me.

Looks like your approach of working with dataGridView is a bit incorrect. 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.

1. First create a class representing your product like:

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.

3. Click on the dataGridView in form designer and note the arrow button at the top right corner. Click on it and generate a new datasource by selecting your Product class. DataGridBindingSource will appear at the bottom of the form designer. Let's assume it's name is dataGridViewBindingSource .

4. Assign your products collection to the binding source:

dataGridViewBindingSource.DataSource = products;

Now you can modify products collection and display updated products in the grid by calling dataGridView.Refresh() method. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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