How to access cell in DataGridView using a Switch statement

I have the following code... currently I am using a switch statement which looks for the content of one of the cells in the DGV... I want to change the switch statement to look for the row number of the DGV, (i.e. the case statement would be "case 0:, case 1:, etc)...  I can't see anyway to get the row number for the switch statement...

Help please?
DataTable dt = new DataTable();
            FbDataAdapter da = new FbDataAdapter("SELECT * from tUploadInfo", mainForm.bookConn);
            da.Fill(dt);  //  fill datatable

            if (dgv1.Rows.Count > 0)
                try {
                    dgv1.Rows.Clear();  //  clear the old crappola out...
                }
                catch (Exception)  //  can't clear the first time... donno why
                {
                    ;
                }

            dgv1.Columns.Clear();
            dgv1.DataSource = dt;

            dgv1.Columns[0].HeaderText = "Listing Service";
            dgv1.Columns[0].MinimumWidth = 120;
            dgv1.Columns[1].HeaderText = "User ID";
            dgv1.Columns[1].MinimumWidth = 140;
            dgv1.Columns[2].HeaderText = "Password";
            dgv1.Columns[2].MinimumWidth = 80;
            dgv1.Columns[3].HeaderText = "FTP Address";
            dgv1.Columns[3].MinimumWidth = 160;
            dgv1.Columns[4].HeaderText = "FTP Directory";
            dgv1.Columns[4].MinimumWidth = 105;
            dgv1.Columns[5].HeaderText = "File Format";
            dgv1.Columns[5].MinimumWidth = 10;

            foreach (DataGridViewRow dataRow in dgv1.Rows) {
                if (dataRow.Cells[0].Value == null)
                    continue;

                switch (dataRow.Cells[0].Value.ToString())  //  move the data to internal strings  <------------------------ TODO
                {
                    case "A1 Books":
                        mainForm.A1BooksUID = dataRow.Cells[1].Value.ToString();
                        mainForm.A1BooksPwd = dataRow.Cells[2].Value.ToString();
                        break;
                    case "ABE":
                        mainForm.ABEUID = dataRow.Cells[1].Value.ToString();
                        mainForm.ABEPwd = dataRow.Cells[2].Value.ToString();
                        break;
                    case "Alibris":
                        mainForm.AlibrisUID = dataRow.Cells[1].Value.ToString();
                        mainForm.AlibrisPwd = dataRow.Cells[2].Value.ToString();
                        break;

Open in new window

rmmarshAsked:
Who is Participating?
 
Carlos VillegasFull Stack .NET DeveloperCommented:
OK, like this?
int myRowIndex = -1;
foreach (DataGridViewRow dataRow in dgv1.Rows) {
	myRowIndex++;

	if (dataRow.Cells[0].Value == null)
	    continue;

	switch (myRowIndex)
	{
	    case 0:
		mainForm.A1BooksUID = dataRow.Cells[1].Value.ToString();
		mainForm.A1BooksPwd = dataRow.Cells[2].Value.ToString();
		break;
	    case 1:
		mainForm.ABEUID = dataRow.Cells[1].Value.ToString();
		mainForm.ABEPwd = dataRow.Cells[2].Value.ToString();
		break;

// etc............

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Row number/index? like this?
            int myRowIndex = -1;
            foreach (DataGridViewRow dataRow in dgv1.Rows) {
                myRowIndex++;

                if (dataRow.Cells[0].Value == null)
                    continue;


                // etc............
            }

Open in new window

0
 
rmmarshAuthor Commented:
No, I need to have the row number/index as part of the Switch statement...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
rmmarshAuthor Commented:
Looks like that might work... let me give it a go and I'll get back to you... thanks...
0
 
rmmarshAuthor Commented:
Thank you...
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Glad to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.