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.
c# wpf datagridview
add a comment |
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.
c# wpf datagridview
add a comment |
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.
c# wpf datagridview
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.
c# wpf datagridview
c# wpf datagridview
edited Jan 19 at 10:03


James Z
11.1k71935
11.1k71935
asked Jan 19 at 7:23


Oxama BaigOxama Baig
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264958%2fhow-to-increase-rows-of-datagridview-dynamically-and-get-the-combobox-values-all%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 19 at 9:13
answered Jan 19 at 9:08


opewixopewix
4,09811236
4,09811236
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264958%2fhow-to-increase-rows-of-datagridview-dynamically-and-get-the-combobox-values-all%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown