循环访问DataGridView单元格
我正在创build一个生成条形码的程序,然后打印出货标签。
我有一个function,允许用户上传到datagrid视图的电子表格。 其中一个列名称是“跟踪号码”。
我希望能够循环遍历每个有跟踪号码的单元格,然后在称为“条形码”的列中生成一个新的单元格中的条形码。
我明白这有一个循环function,但我从来没有使用过。
生成条形码的代码如下,它调用两个类:
Image barc = Rendering.MakeBarcodeImage(txtTrack.Text, int.Parse(txtWidth.Text), true); pictBarcode.Image = barc;
任何帮助将非常感激。 我会高兴地回答任何其他问题。
要遍历每个单元格,可以使用foreach循环
foreach(DataGridViewRow row in yourDataGridView.Rows) { foreach(DataGridViewCell cell in row.Cells) { //do operations with cell } }
您可以使用以下方法循环访问DataGridView
:
foreach (DataGridViewRow row in dgvNameOfYourGrid.Rows) { if (row["Tracking Number"].ToString != "") { string trackingNumber = row.Cells["Tracking Number"].ToString(); // do stuff with the tracking number } }
但要显示另一个单元格中的条形码,您将需要将其转换为DataGridViewImageCell
(或更好的整个列到DataGridViewImageColumn
)。