如何为此代码使用For循环? [英] How can I use For loop for this code?

查看:62
本文介绍了如何为此代码使用For循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。这里有一个PersonelCardClass类。我在这个对象的列表中添加了我的DataGridView Rows.I读取DataGridview直到行的结尾。验证后的每一行,我将每个行单元分配给list中当前对象的特殊属性。现在我想将单元格索引更改为变量(如果可能)而不是数字。



Hi all.there is a "PersonelCardClass" class.I made a list of this object to add my DataGridView Rows in it.I read DataGridview till the end of rows .on each row after validating,I assign every row cell to a special property of current object in list.Now I want change cells index to a variable(if possible) instead of number.

 var RowsList = new List<PersonelCardClass>();
for (int i = 0; i < dataGridView1.MasterTemplate.Rows.Count-1; i++)             
{ 
  var row=dataGridView1.Rows[i]; //current row
  RowsList.Add(new PersonelCardClass() //assign cells to one object for every row
  {
      carddate = row.Cells[0].Value== null ? "": row.Cells[0].Value,   //row:i,culumn0
      reward = row.Cells[1].Value == null ? "" : row.Cells[1].Value,   //row:i,culumn1
      penalti = row.Cells[2].Value== null ? "" : row.Cells[2].Value,   //row:i,column2
      vacationHours = row.Cells[3].Value== "" ?"00:00" :row.Cells[3].Value, //row:i,column3
      overTimeHours = row.Cells[4].Value== "" ? "00:00": row.Cells[4].Value, //row:i,column4
      workHours = row.Cells[5].Value== "" ? "00:00":row.Cells[5].Value,      //row:i,column5
      mrnngEntranceTime = row.Cells[6].Value== "" ? "00:00": new row.Cells[6].Value    //rowi,column6
  } );
                    
}
return RowsList; //return collection of objects

推荐答案

您可以搜索特定行中的控件并为对象属性赋值。



ex:

objPersonelCardClass.carddate = Convert.ToString(((TextBox)row.FindControl(txtCardDate))。Text.Trim());
You can search a control in the specific row and assign values to the object properties.

ex:
objPersonelCardClass.carddate = Convert.ToString(((TextBox)row.FindControl("txtCardDate")).Text.Trim());


这篇关于如何为此代码使用For循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆