如何为此代码使用For循环?

编程入门 行业动态 更新时间:2024-10-28 08:20:45
本文介绍了如何为此代码使用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循环?

本文发布于:2023-10-28 17:41:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!