Quoting through DataGridView cells

I am creating a program that generates a barcode and then prints delivery labels.

I have a function that allows a user to load a spreadsheet into a datagrid view. One of the column names is "Tracking Number."

I would like to be able to scroll through each cell with a tracking number, and then generate a barcode in a new cell in a column called "barcode".

I understand that there is a loop function for this, but I have never used it before.

The code that generates the barcode is as follows: it calls two classes:

Image barc = Rendering.MakeBarcodeImage(txtTrack.Text, int.Parse(txtWidth.Text), true); pictBarcode.Image = barc; 

Any help would be greatly appreciated. I will be happy to answer any other questions.

+7
source share
2 answers

To iterate over each cell, you can use foreach loops

 foreach(DataGridViewRow row in yourDataGridView.Rows) { foreach(DataGridViewCell cell in row.Cells) { //do operations with cell } } 
+15
source

You can execute a DataGridView loop using the following:

 foreach (DataGridViewRow row in dgvNameOfYourGrid.Rows) { if (row["Tracking Number"].ToString != "") { string trackingNumber = row.Cells["Tracking Number"].ToString(); // do stuff with the tracking number } } 

But to display the barcode in another cell, you will need to convert it to a DataGridViewImageCell (or, preferably, the entire column in a DataGridViewImageColumn ).

+1
source

All Articles