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 jobs = new List();
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?
Jayraj ChhayaPosted Dec 1, 2023, 11:53 AM
Problem
The issue you're facing is that only one row is being displayed in the DataGridView, even though there should be multiple rows matching the job number entered.
Cause
The cause of the problem is that the
jobslist is not being cleared before populating it with new data. As a result, the previously added rows remain in the list, and only the first matching row is displayed in the DataGridView.Solution
To fix the issue, you need to clear the
jobslist before populating it with new data. You can do this by addingjobs.Clear()before the foreach loop in thebutton1_Clickevent handler:By clearing the
jobslist before each search, you ensure that only the rows matching the current job number are added to the list.