Hi,
I am working on a form with datagridview and webbrowser controls. I have three columns as URL, username and password in datagridview. What I want to do is to automate the login for some websites that I use frequently. For that reason I am not sure if this is the right approach but I created the below code. The problem is with the argument of switch.
I will click the row on datagridview and then click the login_button so that the username and password info will be passed to the related fields on the webpage. Why I need a switch-case loop is because all the webpages have different element IDs for username and password fields.
As I said, I am not sure if datagridview allows switch-case, I searched the net but couldn't find any samples.
private void login_button_Click(object sender, EventArgs e)
{
switch (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())
{
case "http://www.website1.com":
webBrowser1.Document.GetElementById("username").InnerText = dataGridView1.Rows[3].Cells[3].Value.ToString();
webBrowser1.Document.GetElementById("password").InnerText = dataGridView1.Rows[3].Cells[4].Value.ToString();
return;
case "http://www.website2.com":
webBrowser1.Document.GetElementById("uname").InnerText = dataGridView1.Rows[4].Cells[3].Value.ToString();
webBrowser1.Document.GetElementById("pswd").InnerText = dataGridView1.Rows[4].Cells[4].Value.ToString();
return;
}
HtmlElementCollection elements = this.webBrowser1.Document.GetElementsByTagName("Form");
foreach (HtmlElement currentElement in elements)
{
currentElement.InvokeMember("Login");
}
}

1 answers
The 'switch' statement is part of the C# language and works with integers, characters, strings and enums.
So, whilst it's not wrong in principle to switch on a string value, the problem you've got here is that the EventArgs argument, e, doesn't have RowIndex and ColumnIndex properties because you're clicking a button, not a cell on the DGV!
You therefore need to find some other way of referring to the cell which contains the URL at the time the login button is clicked.
For example, if you have the SelectionMode property set to FullRowSelect or RowHeaderSelect, then you could use the SelectedRows property to get the selected row (check first that there is one to avoid an exception) and then the Cells property of that row to get the cell which contains the URL.
answered one year ago by:
17279