I have the following case :
I add row by row to Datatable dtItems according to the user data entry through a button .
One of the columns in my data table is Hours , and i wanna to achieve the following conditions :
- 1- for each user the total hours is less than or equal 5.
-
2- the default :if the user enter one row then hours = 5
if he enters two rows then make the first one 4 and the second one is 1
if he enters three rows then make the first one is 3 and the second is 1 and the third is 1.
etc.
- 3-the maximum number of rows for each user is 5.
LIKE this:
user_id | name | hours
323 | jo | 3
323 | jo | 1
323 | jo | 1
324 | jack | 4
324 | jack | 1
DataTable dtItems = GetDataTable();
DataRow dr = dtItems.NewRow();
dr["emp_num"] = txt_EmpNum.Text.Trim();
dr["name"] = txt_EmpName.Text.Trim();
dr["hours"] = 5;
dtItems.Rows.Add(dr);
GV_Employee.DataSource = dtItems;
GV_Employee.DataBind();
Session["ItemDT"] = dtItems;
I assume that you don’t know how to change the DataRow’s
Hourfields accordingly before you insert them into database.The only what you need to know is the new
hourof the first DataRow, the others get 1 hour:To prevent users from inserting more than 5 rows, you only need to check for
dtItems.Rows.Count < 5before you insert the new.Edit: If you need it to be calculated for every
emp_numin theDataTableas commented: