C#动态复选框”;检查”;

本文关键字:检查 复选框 动态 | 更新日期: 2023-09-27 18:22:24

我有一个for循环,用于将5个动态创建的文本框发送到Excel中的一行。这个功能非常完美,但是。。。我还有一个复选框,它是与每一行一起创建的,目前被忽略。我想将此复选框添加到我的函数中。我只希望选中复选框的动态行转到Excel。下面是我现有的for循环代码。这是复选框("chkAddItem")的名称

 for (int CheckRow = 0; CheckRow < addOnRows; CheckRow++)
        {                                                   //add addOn rows to spreadsheet
            worksheet.Rows[StartAddOn].Insert();
            worksheet.Cells[StartAddOn, "D"].value = srcAddOnPanel.Controls["txtQtyAddOn" + CheckRow].Text;
            worksheet.Cells[StartAddOn, "E"].value = srcAddOnPanel.Controls["txtProductNameAddOn" + CheckRow].Text;
            worksheet.Cells[StartAddOn, "F"].value = srcAddOnPanel.Controls["txtListPriceAddOn" + CheckRow].Text;
            worksheet.Cells[StartAddOn, "G"].value = srcAddOnPanel.Controls["txtMaxDiscountAddOn" + CheckRow].Text;
            worksheet.Cells[StartAddOn++, "H"].value = srcAddOnPanel.Controls["txtProposedPriceAddOn" + CheckRow].Text;
        }

C#动态复选框”;检查”;

这样?

for (int checkRow = 0; checkRow < addOnRows; checkRow++)
{      
   if (((CheckBox)srcAddOnPanel.Controls["chkAddItem" + checkRow]).Checked)
   {                                     
      //add addOn rows to spreadsheet
      worksheet.Rows[StartAddOn].Insert();
      worksheet.Cells[StartAddOn, "D"].value = srcAddOnPanel.Controls["txtQtyAddOn" + checkRow].Text;
      worksheet.Cells[StartAddOn, "E"].value = srcAddOnPanel.Controls["txtProductNameAddOn" + checkRow].Text;
      worksheet.Cells[StartAddOn, "F"].value = srcAddOnPanel.Controls["txtListPriceAddOn" + checkRow].Text;
      worksheet.Cells[StartAddOn, "G"].value = srcAddOnPanel.Controls["txtMaxDiscountAddOn" + checkRow].Text;
      worksheet.Cells[StartAddOn++, "H"].value = srcAddOnPanel.Controls["txtProposedPriceAddOn" + checkRow].Text;
   }
}