Looping through DataGridView Cells

心不动则不痛 提交于 2019-12-30 18:48:10

问题


I am creating a program that generates a bar code and then prints the shipping labels.

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

I would like to be able to loop through each cell that has a tracking number and then generate a barcode into a a new cell in a column called "barcode".

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

The code that generates the barcode is the following , it calls two classes:

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

Any Help would be much appreciated. I will happily answer any other questions.


回答1:


To iterate through every cell, you can use foreach loops

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



回答2:


You can loop through a DataGridView 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 preferable the whole column into a DataGridViewImageColumn).



来源:https://stackoverflow.com/questions/13788156/looping-through-datagridview-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!