简体   繁体   English

C#动态复选框“已选中”

[英]C# dynamic checkbox “checked”

I have a for loop to send 5 dynamically created textboxes to a row in Excel. 我有一个for循环,可将5个动态创建的文本框发送到Excel中的一行。 This function works perfect but...I also have a checkbox that is created with each row and is currently ignored. 该功能可以正常工作,但是...我还有一个复选框,该复选框随每行创建,当前被忽略。 I want to add this checkbox to my function. 我想将此复选框添加到我的函数中。 I only want the dynamic rows that have the checkbox selected to go to Excel. 我只希望选择了复选框的动态行进入Excel。 Below is my existing for loop code. 以下是我现有的for循环代码。 This is the name of the checkbox (""chkAddItem") 这是复选框的名称(““ 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;
        }

Like this? 像这样?

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;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM