Answered by:
Hiding a column

Question
-
User-1767698477 posted
Is it possible to make a column appear only upon a test condition? I tried to do this in my row databound event, and I was able to make it appear, but it chopped off the first last column in the gridview. Is there a way to do this without chopping off the last column? I want the textbox box to appear if "Other" is selected from the dropdown selector in column 1. But if non of the dropdown selectors in the gridview have the Other selection made, then the column remains hidden.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim showothercol As Boolean = False
If e.Row.RowType = DataControlRowType.DataRow Then
'e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
If e.Row.Cells(1).Text = "Other" Then
showothercol = True
End If
Dim Liabtype As String = e.Row.Cells(1).Text
If Liabtype = "C" Then
e.Row.Cells(1).Font.Size = 10
e.Row.Cells(1).Text = "Collections, Judgments, & Liens"
End If
If Liabtype = "H" Then
e.Row.Cells(1).Text = "Home Equity Line of Credit"
End If
If Liabtype = "I" Then
e.Row.Cells(1).Text = "Installment"
End If
If Liabtype = "L" Then
e.Row.Cells(1).Text = "Lease"
End If
If Liabtype = "M" Then
e.Row.Cells(1).Text = "Mortgage"
End If
If Liabtype = "O" Then
e.Row.Cells(1).Text = "Open 30-day charge Account"
End If
If Liabtype = "OT" Then
e.Row.Cells(1).Text = "Other"
End If
If Liabtype = "R" Then
e.Row.Cells(1).Text = "Revolving"
End If
If Liabtype = "T" Then
e.Row.Cells(1).Text = "Taxes"
End If
If Liabtype = "TL" Then
e.Row.Cells(1).Text = "Tax Lien"
End If'paid at close
Dim var1 As String = e.Row.Cells(8).Text
If var1 = "True" Then
e.Row.Cells(8).Text = "Y"
ElseIf e.Row.Cells(8).Text = "False" Then
e.Row.Cells(8).Text = ""
End If
'paid ptc
Dim var2 = e.Row.Cells(9).Text
If var2 = "True" Then
e.Row.Cells(9).Text = "Y"
ElseIf e.Row.Cells(9).Text = "False" Then
e.Row.Cells(9).Text = ""
End If
'resubordinate
Dim var3 = e.Row.Cells(10).Text
If var3 = "True" Then
e.Row.Cells(10).Text = "Y"
ElseIf e.Row.Cells(10).Text = "False" Then
e.Row.Cells(10).Text = ""
End If
'omit
Dim var4 = e.Row.Cells(11).Text
If var4 = "True" Then
e.Row.Cells(11).Text = "Y"
ElseIf e.Row.Cells(11).Text = "False" Then
e.Row.Cells(11).Text = ""
End If
If showothercol = True Then
e.Row.Cells(2).Visible = True
Else
e.Row.Cells(2).Visible = False
End If
End If
End Sub<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellPadding="3" Caption="Liabilities" CaptionAlign="Top" HorizontalAlign="Center" OnRowDataBound="Gridview1_rowdatabound" RowStyle-Wrap="False" HeaderStyle-Wrap="False"> <PagerStyle Wrap="True" /> <RowStyle HorizontalAlign="Center" /> <Columns> <asp:TemplateField ItemStyle-Width="30px" HeaderText=""> <ItemTemplate> <asp:ImageButton ID="imgbtn" ImageUrl="~/templates/images/small-pencil.jpg" runat="server" Width="25" Height="25" OnClick="imgbtn_Click" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> <asp:BoundField DataField="Othertype" HeaderText="Other type" SortExpression="Othertype" /> <asp:BoundField DataField="Name" HeaderText="Company" SortExpression="Name" /> <asp:BoundField DataField="Acctnum" HeaderText="Acct #" SortExpression="Acctnum" /> <asp:BoundField DataField="Mopmt" HeaderText="Payment" SortExpression="Mopmt" /> <asp:BoundField DataField="Balance" HeaderText="Balance" SortExpression="Balance" /> <asp:BoundField DataField="Molefttopay" HeaderText="Months left" SortExpression="Molefttopay" /> <asp:BoundField DataField="Paidatclosing" HeaderText="Paid @ close" SortExpression="Paidatclosing" /> <asp:BoundField DataField="PaidPTC" HeaderText="Paid PTC" SortExpression="PaidPTC" /> <asp:BoundField DataField="Resub" HeaderText="Resubordinate" SortExpression="Resub" /> <asp:BoundField DataField="Omit" HeaderText="Omit" SortExpression="Omit" /> <asp:BoundField DataField="REOnum" HeaderText="REO #" SortExpression="REOnum" /> </Columns> </asp:GridView>
Friday, May 7, 2021 11:54 PM
Answers
-
User1535942433 posted
Hi ,
If you want to hide a column in the RowDataBound, you could use like this:
If e.Row.Cells(1).Text = "Other" Then e.Row.Cells(index).Visible = false; End If
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 10, 2021 6:38 AM