Answered by:
Grid view row in For each loop-VB.NET

Question
-
User-1578974752 posted
For Each item As GridViewRow In kgrid.Rows
If item.RowType = DataControlRowType.DataRow Then
----
----
----
----If ((boxa.Text * boxb.Text) = box.Text Then
Update statementcmd.ExecuteNonQuery()
elseLabel1.text ="not saved"
Next
I have label and textbox ,in the template column of the grid view.if there is 5 rows in the grid view ,If update statement not executed for any one of the rows then ,i have to show a message "not saved"Now it is showing only if the last row is not executing the update statement . What i need is any one of the row is not executed then this msg must show.
For eg: If first row didn't meet the If ((boxa.Text * boxb.Text) = box.Text Then condition,then it will go through the Label1.text ="not saved",but after -> Next statement ,the row is executing and if it is saved then else portion will not work and "Not Saved " message is not showingHow can I do such that any one of the grid view rows is not executing the update statement ,"not saved" must show.
Appreciate hte help
Wednesday, January 23, 2019 10:55 AM
Answers
-
User-893317190 posted
Hi shsu,
Then you could first set the flag's value to true, in your every for loop, you could only set it to false, when the loop ends,you could decide whether to bind the gridview according to the flag.
Dim flag as Boolean = true ' first it is true For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then If ((boxa.Text * boxb.Text) = box.Text Then Update statement cmd.ExecuteNonQuery() else flag = false ' update the flag and only set it to false so that only one row is ' invalid , the flag will be false and in the for loop , it couldn't ' be true Next If flag Then 'bind your gridview End If
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, January 25, 2019 1:10 AM
All replies
-
User-943250815 posted
If I understand correctly you should do it on Gridview.RowDataBound event.
To get or set controls value you have to use Row.Cells.FindcontrolFor Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then Dim boxa as Label = e.row.cells(0).findcontrol("boxa") Dim boxb as Label = e.row.cells(1).findcontrol("boxb") Dim box as Label = e.row.cells(2).findcontrol("box") Dim label as Label = e.row.cells(3).findcontrol("label") if CInt(boxa.Text) * CInt(boxb.Text) = CInt(box.Text) then
' Do your update
label.Text = "updated" else
label.Text = "not saved" endif endif NextWednesday, January 23, 2019 12:20 PM -
User-1578974752 posted
There are a lot validation in the save button, and this code I can place only in save button.
Thursday, January 24, 2019 1:29 AM -
User-893317190 posted
Hi shsu,
I'm a little confused by your requirement.
Why do you want to execute Label1.text ="not saved" every time when any of the row meets the condition ((boxa.Text * boxb.Text) = box.Text instead of according to the current rows ((boxa.Text * boxb.Text) = box.Text?
Anyway, if you want to meet the requirement, I suggest you could add a flag variable above your for each code.
For example, you could write your code as follows.
Dim flag as Boolean = false ' first it is false
For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then If ((boxa.Text * boxb.Text) = box.Text Then Update statement cmd.ExecuteNonQuery() else flag = true ' update the flag
End If
If flag Then
Label1.text ="not saved" ' execute according to whether flag is true
End If
NextBut this could only applies to the following rows.
If you want to apply to every row , you could make another for loop if the flag if true.
For example , you could
Dim flag as Boolean = false ' first it is false For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then If ((boxa.Text * boxb.Text) = box.Text Then Update statement cmd.ExecuteNonQuery() else flag = true ' update the flag Next If flag Then For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then Label1.text ="not saved" Next End If
Best regards,
Ackerly Xu
Thursday, January 24, 2019 2:33 AM -
User-1578974752 posted
Thanks
Actually the scenario is like this
If there are 10 rows.
If the formula is true and validate properly then only it will execute the Update statement.
If any one row is Not successful with the validation then, that row must show as editable(label invisble and textbox visible),upon clicking the save button
If all rows validation successful then gridview1.databind() command will work. The problem I am facing is ,If I place the gridview1.databind() after the
cmd.ExecuteNonQuery(), then whole gridview is refreshing and label invisble and textbox visible will not work.
If flag is true then
Gridview1.databind()
Now in your code the flag is updating each time with in the for.. Next loop right..so if the last row's flag update is true then Gridview1.databind() will work. I want if any one row is not updated due to validation failure,then flag must be false and Gridview1.databind() will not work.
Thursday, January 24, 2019 6:55 AM -
User-893317190 posted
Hi shsu,
Then you could first set the flag's value to true, in your every for loop, you could only set it to false, when the loop ends,you could decide whether to bind the gridview according to the flag.
Dim flag as Boolean = true ' first it is true For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then If ((boxa.Text * boxb.Text) = box.Text Then Update statement cmd.ExecuteNonQuery() else flag = false ' update the flag and only set it to false so that only one row is ' invalid , the flag will be false and in the for loop , it couldn't ' be true Next If flag Then 'bind your gridview End If
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, January 25, 2019 1:10 AM