[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

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

0
rmmarsh
Asked:
rmmarsh
  • 3
  • 3
1 Solution
 
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
 
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now