如何根据某些条件更改数据表中特定列的数据
我有以下情况:
我根据通过按钮输入的用户数据逐行添加到Datatable dtItems
。
我的数据表中的一列是 Hours
,我想实现以下条件:
- 1-对于每个用户,总小时数小于或等于 5。
2-默认值:如果用户输入一行,则小时 = 5
如果他输入两行,则将第一行设为 4,第二行设为 1
如果他输入三行,则将第一行设为 3,第二行设为 1,第三行设为 1。
etc.
- 3-每个用户的最大行数是 5。
如下所示:
user_id |名称 |小时
323 |乔 | 3
323 |乔 | 1
323 |乔 | 1
324 |杰克| 4
324 |杰克| 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 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您在将 DataRow 的
Hour
字段插入数据库之前不知道如何相应地更改它们。您唯一需要知道的是第一个 DataRow 的新
hour
,其他 DataRow 为 1 小时:为了防止用户插入超过 5 行,您只需检查
dtItems。行数< 5
在插入新的之前。编辑:如果您需要为
DataTable
中的每个emp_num
进行计算,如注释所示:I assume that you don't know how to change the DataRow's
Hour
fields accordingly before you insert them into database.The only what you need to know is the new
hour
of 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 < 5
before you insert the new.Edit: If you need it to be calculated for every
emp_num
in theDataTable
as commented: