【问题标题】:Looping through DataGridView Cells循环遍历 DataGridView 单元格
【发布时间】:2012-12-09 14:12:15
【问题描述】:

我正在创建一个程序来生成条形码,然后打印运输标签。

我有一个允许用户将电子表格上传到数据网格视图的功能。列名称之一是“跟踪号”。

我希望能够遍历每个具有跟踪号的单元格,然后将条形码生成到名为“条形码”的列中的新单元格中。

我知道有一个循环功能,但我以前从未使用过。

生成条码的代码如下,它调用了两个类:

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

任何帮助将不胜感激。我很乐意回答任何其他问题。

【问题讨论】:

  • 我对你的确切问题有点模糊。您在显示图像时遇到问题吗?如果是这样,应该显示条形码图像的列是DataGridViewImageColumn?还是您需要帮助循环遍历 DataGridView?

标签: c# visual-studio-2010 excel loops datagridview


【解决方案1】:

要遍历每个单元格,您可以使用 foreach 循环

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

【讨论】:

    【解决方案2】:

    您可以使用以下代码循环访问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)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      相关资源
      最近更新 更多