Click here to Skip to main content
15,921,279 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.

C#
 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
Posted
Updated 10-Feb-14 6:09am
v4
Comments
OriginalGriff 10-Feb-14 11:45am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
You existing code uses an index in a for loop...
Use the "Improve question" widget to edit your question and provide better information.
mit62 10-Feb-14 11:46am    
ok
BillWoodruff 10-Feb-14 12:24pm    
Does this code "work" right now ? Please show the code for the constructor of the 'PersonelCardClass: based on that I may have some suggestions.
mit62 10-Feb-14 12:32pm    
Yes this is work good.it hasn't any constructor
BillWoodruff 10-Feb-14 12:45pm    
Okay, but are you sure 'PersonelCardClass has NO constructor ? If that's the case I predict the code sample above does not compile: are you saying it does compile ? What error messages are you getting ?

1 solution

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());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900