Joseph Fisher

Joseph Fisher

  • 1.7k
  • 6
  • 387

Issue with getting foreach loop to work

Nov 30 2023 12:34 PM
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace CEM_Label_Creator
{
    public partial class Label_Creator_Form : Form
    {
        public static List<JobData> jobs = new List<JobData>();
        
        public Label_Creator_Form()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //looks at input boxes for data that matches JB database, if match is found add a row to the DGV
            foreach (DataRow row in PRODUCTIONDataSet.Job.Rows)
            {
                string rowValue = row["Job"].ToString();
                MessageBox.Show(rowValue);
                if (rowValue == txtbox_Job.Text)
                {
                    JobData job = new JobData();
                    job.JobNumber = (string)row["Job"];
                    job.OrderQty = (int)row["Order_Quantity"];
                    job.ExtDesc = (string)row["Ext_Description"];
                    jobs.Add(job);
                }
            }

            label_DGV.DataSource = jobs;
            label_DGV.Refresh();




        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Adds data from each row into a list, then apply that list to a print function
            //finally clear out the DGV and inputs if needed,await for next label to be created
            foreach (DataGridViewRow row in label_DGV.Rows)
            {

            }



            label_DGV.Rows.Clear();
            label_DGV.Refresh();
        }

        private void Label_Creator_Form_Load(object sender, EventArgs e)
        {

        }

        public class JobData
        {
            public string JobNumber { get; set; }
            public int OrderQty { get; set; }
            public string ExtDesc { get; set; }

        }
    }
}

Hello,

I am trying to match data within a dataset on a SQL database with text in a input box.  the user types in a job number then it should search through all jobs in the datatable for a match, when it finds a match i pull specific fields and add them to a list. i then make the list the datasource for a DGV. I believe my loop is wrong as i can see the DGV has the proper headers based on my list. the list is just empty.

I have very limited experience messing with databases and tables. so im just not sure if im accessing it correctly.

**EDIT**

I figured out that i had 2 seperate connections and i was accessing the wrong one. but now with the same code, accessing the proper connection, i can get a row displayed. but only 1 it wont let me do another row. any insight?


Answers (1)