Answered by:
Colour change in grid view -vb.net

Question
-
User-1578974752 posted
In the below code first portion is working. If "OPen" then colour is shoiwng as red,SkyBlue and yellow. But ponumtxt.Text(dropdownlist) when changed to PENDING or closed,then colour change is not working.After changing the status If I come back to OPEN status also,colour change not showing for the rows . I want all these 3 to work (OPEN,PENDING,CLOSED). Appreciate the Help
For Each item As GridViewRow In kgrid.Rows
If item.RowType = DataControlRowType.DataRow Then
Dim box As TextBox = CType(item.FindControl("TextBox11"), TextBox)
If ponumtxt.Text = "OPEN" ThenIf box.Text = "ABC" Then
item.BackColor = System.Drawing.Color.Red
ElseIf box.Text = "EFG" Then
item.BackColor = System.Drawing.Color.SkyBlue
box.BackColor = System.Drawing.Color.SkyBlueElseIf box.Text = "IJK" Then
item.BackColor = System.Drawing.Color.Yellow
box.BackColor = System.Drawing.Color.Yellow
End If
'End If
'end change
If ponumtxt.Text = "PENDING" Then
If box.Text = "ABC" Then
' If Val(potext.Text) = 31 Then
item.BackColor = System.Drawing.Color.Redbox.BackColor = System.Drawing.Color.Red
ElseIf box.Text = "EFG" Then
item.BackColor = System.Drawing.Color.SkyBluebox.BackColor = System.Drawing.Color.SkyBlue
ElseIf box.Text = "IJK" Then
item.BackColor = System.Drawing.Color.Yellowbox.BackColor = System.Drawing.Color.Yellow
End If
End If
If ponumtxt.Text = "CLOSED" Then
If box.Text = "ABC" Thenitem.BackColor = System.Drawing.Color.Red
box.BackColor = System.Drawing.Color.RedElseIf box.Text = "EFG" Then
item.BackColor = System.Drawing.Color.SkyBluebox.BackColor = System.Drawing.Color.SkyBlue
ElseIf box.Text = "IJK" Then
item.BackColor = System.Drawing.Color.BlueViolet
box.BackColor = System.Drawing.Color.BlueViolet
End If
End If
End If
Next
End SubTuesday, January 29, 2019 3:20 AM
Answers
-
User-893317190 posted
Hi shsu,
As jzero has said, the way your IFs are arranged "PENDING" and "CLOSED" will not be verified.
I have tried your code and made a little change, you could have a refer .Please pay attention I set the dropdown's AutoPostBack property to true.
<form id="form1" runat="server"> <asp:GridView ID="kgrid" runat="server" OnDataBound="kgrid_DataBound" AutoGenerateColumns="false" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TextBox11" runat="server" Text="<%# GetDataItem().ToString() %>"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text="the second column"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:DropDownList ID="ponumtxt" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem>OPEN</asp:ListItem> <asp:ListItem>PENDING</asp:ListItem> <asp:ListItem>CLOSED</asp:ListItem> </asp:DropDownList> </form>
Code behind. Please pay attention I write both the code in the dropdown's SelectedIndexChanged event and the gridview's DataBound event, becauce DataBound event doesn't always trigger in every post back.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then kgrid.DataSource = New String() {"ABC", "EFG", "IJK", "LMN"} kgrid.DataBind() End If End Sub Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then Dim box As TextBox = CType(item.FindControl("TextBox11"), TextBox) 'if is open start If ponumtxt.Text = "OPEN" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.White ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' 'if is open end ' if is PENDING start If ponumtxt.Text = "PENDING" Then If box.Text = "ABC" Then ' If Val(potext.Text) = 31 Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' if is PENDING end 'if is CLOSED start If ponumtxt.Text = "CLOSED" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.BlueViolet box.BackColor = System.Drawing.Color.BlueViolet End If End If ' if is CLOSED end End If Next End Sub Protected Sub kgrid_DataBound(sender As Object, e As EventArgs) For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then Dim box As TextBox = CType(item.FindControl("TextBox11"), TextBox) 'if is open start If ponumtxt.Text = "OPEN" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' 'if is open end ' if is PENDING start If ponumtxt.Text = "PENDING" Then If box.Text = "ABC" Then ' If Val(potext.Text) = 31 Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' if is PENDING end 'if is CLOSED start If ponumtxt.Text = "CLOSED" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.BlueViolet box.BackColor = System.Drawing.Color.BlueViolet End If End If ' if is CLOSED end End If Next End Sub
The result.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 31, 2019 4:35 AM
All replies
-
User-943250815 posted
Are you sure your IFs are correct?
A short version of your code is like thisIf ponumtxt.Text = "OPEN" Then some code here If ponumtxt.Text = "PENDING" Then some code here End If If ponumtxt.Text = "CLOSED" Then some code here End If End If
It was not supposed be?
If ponumtxt.Text = "OPEN" Then some code here End If If ponumtxt.Text = "PENDING" Then some code here End If If ponumtxt.Text = "CLOSED" Then some code here End If
Tuesday, January 29, 2019 8:34 PM -
User-1578974752 posted
Only open IS Working anf that too ,After changing the status If I come back to OPEN status also, colour change not showing for the rows . I am not using paging istead using DIV for scrolling
Wednesday, January 30, 2019 9:06 AM -
User-943250815 posted
shsu,
The way your IFs are arranged "PENDING" and "CLOSED" will not be verified, that's why I asked if they are correct and proposed another arrangementWednesday, January 30, 2019 7:16 PM -
User-893317190 posted
Hi shsu,
As jzero has said, the way your IFs are arranged "PENDING" and "CLOSED" will not be verified.
I have tried your code and made a little change, you could have a refer .Please pay attention I set the dropdown's AutoPostBack property to true.
<form id="form1" runat="server"> <asp:GridView ID="kgrid" runat="server" OnDataBound="kgrid_DataBound" AutoGenerateColumns="false" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TextBox11" runat="server" Text="<%# GetDataItem().ToString() %>"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text="the second column"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:DropDownList ID="ponumtxt" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem>OPEN</asp:ListItem> <asp:ListItem>PENDING</asp:ListItem> <asp:ListItem>CLOSED</asp:ListItem> </asp:DropDownList> </form>
Code behind. Please pay attention I write both the code in the dropdown's SelectedIndexChanged event and the gridview's DataBound event, becauce DataBound event doesn't always trigger in every post back.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then kgrid.DataSource = New String() {"ABC", "EFG", "IJK", "LMN"} kgrid.DataBind() End If End Sub Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then Dim box As TextBox = CType(item.FindControl("TextBox11"), TextBox) 'if is open start If ponumtxt.Text = "OPEN" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.White ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' 'if is open end ' if is PENDING start If ponumtxt.Text = "PENDING" Then If box.Text = "ABC" Then ' If Val(potext.Text) = 31 Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' if is PENDING end 'if is CLOSED start If ponumtxt.Text = "CLOSED" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.BlueViolet box.BackColor = System.Drawing.Color.BlueViolet End If End If ' if is CLOSED end End If Next End Sub Protected Sub kgrid_DataBound(sender As Object, e As EventArgs) For Each item As GridViewRow In kgrid.Rows If item.RowType = DataControlRowType.DataRow Then Dim box As TextBox = CType(item.FindControl("TextBox11"), TextBox) 'if is open start If ponumtxt.Text = "OPEN" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' 'if is open end ' if is PENDING start If ponumtxt.Text = "PENDING" Then If box.Text = "ABC" Then ' If Val(potext.Text) = 31 Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.Yellow box.BackColor = System.Drawing.Color.Yellow End If End If ' if is PENDING end 'if is CLOSED start If ponumtxt.Text = "CLOSED" Then If box.Text = "ABC" Then item.BackColor = System.Drawing.Color.Red box.BackColor = System.Drawing.Color.Red ElseIf box.Text = "EFG" Then item.BackColor = System.Drawing.Color.SkyBlue box.BackColor = System.Drawing.Color.SkyBlue ElseIf box.Text = "IJK" Then item.BackColor = System.Drawing.Color.BlueViolet box.BackColor = System.Drawing.Color.BlueViolet End If End If ' if is CLOSED end End If Next End Sub
The result.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 31, 2019 4:35 AM