【问题标题】:How to print values from array being 2 Row per page in C#如何在 C# 中打印数组中每页 2 行的值
【发布时间】:2015-09-28 17:38:18
【问题描述】:

大家好,我又来了!

我的问题是如何从同一文档中的数组中打印一个序号,因为每页的序号最大值为 2

主场景只打印数据网格视图中选中的行! 当我检查它添加到数组中的行并打印时,但我需要在数组中获取第一个和第二个值并在同一页面上打印,即使选中的行超过两个。如果我有 6 行检查,那么打印将打印 3 页

我的数据网格视图

checked row = value1
checked row = value2
unchecked row = value3
unchecked row = value4
checked row = value5
unchecked row = value6
unchecked row = value7
checked row = value8
unchecked row = value9
checked row = value10
checked row = value11



Exemple: in the array contains= value1,value2, value5,value8         value10,value11

Document 1 
Page 1
______________________
Value 1       Value 2
______________________
Document 1
Page 2
______________________
Value 5       Value 8
______________________
Document 1
Page 3
______________________
Value 10       Value 11

提前谢谢各位,可以根据需要随意格式化这个问题!

【问题讨论】:

  • 您可以使用此答案在每页打印2个标签,注意d在标签宽度的代码中。

标签: c# arrays winforms printing datagridview


【解决方案1】:
    using IDAutomation.NetAssembly;  

    private int currentPrintingIndex = 0;       

    List<string> CheckedValues = new List<string>();

    private void printDocument1_PrintPage(object sender,       System.Drawing.Printing.PrintPageEventArgs e)
    {
     Font fbarra = new Font("IDAutomationSC128S", 10, FontStyle.Regular, GraphicsUnit.Pixel);

 IEnumerable<DataGridViewRow> CheckedValues =    this.dgv1.Rows.Cast<DataGridViewRow>()
        .Where(row => (bool?)row.Cells[0].Value == true)
        .Skip(currentPrintingIndex);

      IEnumerator<DataGridViewRow> cve = CheckedValues.GetEnumerator();
      int count = 0;
      int pos = 60;

      while ((e.HasMorePages = cve.MoveNext()) && count++ < 2)
      {
       var cellValues = cve.Current.Cells.Cast<DataGridViewCell>()
                    .Skip(1)    //instead of .Where(cell => cell.ColumnIndex > 0)
                    .Select(cell => cell.Value.ToString())
                    .ToArray();



       StringBuilder builder = new StringBuilder();

       builder.Append(string.Join(",", cellValues));

       string fullline = builder.ToString();
       string[] column1 = fullline.Split(',');


          var cents = column1[3].Substring(0, 2);
          var descr = column1[1].ToString();

          var descr2 = descr.Substring(0, 30);
          var descr3 = descr.Substring(30);

          var encodeddata1 = Encode.Code128(column1[0].ToString(), 0, false);
          var number = encodeddata1;

 e.Graphics.DrawString(number, fbarra, Brushes.Black, pos, 105);//Barcode

 currentPrintingIndex += 1;

 pos += 150;

【讨论】:

    【解决方案2】:

    这是一个使用List&lt;string&gt; 完成您需要的代码,您可以简单地使用List&lt;DataGridViewRow&gt; 而不是List&lt;string&gt;

    List<string> CheckedValues = new List<string>();
    private void button1_Click(object sender, EventArgs e)
    {
        CheckedValues = new List<string> { "value1", "value2", "value5", "value8", "value10", "value11" };
        printDocument1.Print();
    }
    
    int currentPrintingIndex = 0;
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        //Width of output labels
        var d= this.printDocument1.DefaultPageSettings.PrintableArea.Width / 2;
    
        //Print first output label
        if (CheckedValues.Count > currentPrintingIndex)
        {
            var currentValue = CheckedValues[currentPrintingIndex];
            e.Graphics.DrawString(currentValue.ToString(),
                        this.Font,
                        new SolidBrush(this.ForeColor),
                        new RectangleF(
                            0,
                            0,
                            d,
                            this.printDocument1.DefaultPageSettings.PrintableArea.Height));
    
            currentPrintingIndex += 1;
        }
    
        //Print second output label
        if (CheckedValues.Count > currentPrintingIndex)
        {
            var currentValue = CheckedValues[currentPrintingIndex];
            e.Graphics.DrawString(currentValue.ToString(),
                        this.Font,
                        new SolidBrush(this.ForeColor),
                        new RectangleF(
                            d,
                            0,
                            d,
                            this.printDocument1.DefaultPageSettings.PrintableArea.Height));
    
            currentPrintingIndex += 1;
        }
    
        //If there is more item to print, go to next page
        e.HasMorePages = CheckedValues.Count > currentPrintingIndex;
    }
    

    输出将是:

    Page 1
    value 1                                    value 2
    
    Page 2
    value 5                                    value 8
    
    Page 3
    value 10                                   value 11
    

    这背后的逻辑很简单。我们想在 2 个输出列中显示数据。所以我们应该检查我们是否不在 Checked Cells List 的末尾,打印第一行,然后再次检查我们是否不在 Checked Cells List 的末尾打印第二行,然后检查选中的单元格列表中是否有更多项目,所以我们说使用e.HasMorePages = CheckedValues.Count &gt; currentPrintingIndex; 转到下一页并将打印指针移动到下一个选中的单元格。

    编辑


    这里是 Elton Joani 编辑的终极解决方案

    private int currentPrintingIndex = 0;       
    List<string> CheckedValues = new List<string>();
    
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        //Barcode Font
        Font fbarra = new Font("IDAutomationSC128S", 10, FontStyle.Regular, GraphicsUnit.Pixel);
    
        IEnumerable<DataGridViewRow> CheckedValues = this.dgv1.Rows.Cast<DataGridViewRow>()
        .Where(row => (bool?)row.Cells[0].Value == true)
        .Skip(currentPrintingIndex);
    
        IEnumerator<DataGridViewRow> cve = CheckedValues.GetEnumerator();
        int count = 0;
        int pos = 60;
    
        while ((e.HasMorePages = cve.MoveNext()) && count++ < 2)
        {
            var cellValues = cve.Current.Cells.Cast<DataGridViewCell>()
                        .Skip(1)    //instead of .Where(cell => cell.ColumnIndex > 0)
                        .Select(cell => cell.Value.ToString())
                        .ToArray();
    
    
            StringBuilder builder = new StringBuilder();
            builder.Append(string.Join(",", cellValues));
    
            string fullline = builder.ToString();
            string[] column1 = fullline.Split(',');
    
            var cents = column1[3].Substring(0, 2);
            var descr = column1[1].ToString();
            var descr2 = descr.Substring(0, 30);
            var descr3 = descr.Substring(30);
    
            var encodeddata1 = Encode.Code128(column1[0].ToString(), 0, false);
            var number = encodeddata1;
    
            //draw barcode
            e.Graphics.DrawString(number, fbarra, Brushes.Black, pos, 105);
            currentPrintingIndex += 1;
    
            pos += 150;
        }
    }
    

    【讨论】:

    • 嗨,Reza Aghaei!我尝试了很多时间来使这段代码适应我的。但是一旦值来自datagridview并且在我发送到1个数组之后,我无法弄清楚如何保持相同的想法,在这种情况下,我需要为每2个选中的行执行此操作,并为第一个和其他创建一个数组数组到秒
    • @EltonJoani 首先,您应该完全使用此示例代码来了解答案背后的逻辑。然后您可以简单地将其转换为您的具体情况。
    • @EltonJoani 虽然此处提供的帖子回答了这个问题,但我正在等待听到你的好消息。我更新了答案,在答案末尾添加了一些描述。
    • 嗨,Reza Aghaei,我完全按照您的说法做了,复制并尝试了解此代码,我得到了部分内容,除了代码在哪里 (CheckedValues = new List { "value1 ", "value2", "value5", "value8", "value10", "value11" };).....我试图设置一个新数组来替换这个值,但我没有成功!剩下的我就知道它是如何工作的了!
    • @EltonJoani 而不是这个List&lt;string&gt;,您可以简单地使用已检查行的列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多