Solved

ASP.net Make Cell Blank for certain rows

Posted on 2015-01-20
3
237 Views
Last Modified: 2015-01-21
Hi

I am trying to make my GridView radio button cells blank for certain rows using the code below but as the image shows
hiding them causes the cells to shift to the right. I don't want the cells to shift. How do I achieve this
The GridView markup is shown at the bottom

  Private Sub GridView_Questions_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Questions.RowCreated
        Try

            If e.Row.RowIndex = 0 Then
                e.Row.Cells(4).Visible = False
            End If

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

1
<asp:GridView ID="GridView_Questions" runat="server" BackColor="White"
                BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
                Height="147px" Visible="False" Width="795px">
                <Columns>
       
                               <asp:TemplateField HeaderText="Questions: read carefully and select an answer">
                                   <ControlStyle Height="220px" Width="700px" />
                                   <HeaderStyle HorizontalAlign="Left" Wrap="False" />

                                   <ItemStyle Wrap="True" />

                               </asp:TemplateField>
         
                               <asp:ImageField DataImageUrlField="Image" HeaderText="Image">
                               </asp:ImageField>
                               <asp:TemplateField HeaderText="1" ItemStyle-HorizontalAlign="Right">
                                   <ItemTemplate>
                                       <asp:RadioButtonList ID="RadioButtonList1" runat="server"
                                           RepeatDirection="Horizontal">
                                           <asp:ListItem Value="1">Yes</asp:ListItem>
                                           <asp:ListItem Value="0">No</asp:ListItem>
                                       </asp:RadioButtonList>
                                   </ItemTemplate>
                                   <ItemStyle HorizontalAlign="Left" />
                               </asp:TemplateField>
                               <asp:TemplateField HeaderText="2">
                                   <ItemTemplate>
                                       <asp:RadioButtonList ID="RadioButtonList2" runat="server"
                                           RepeatDirection="Horizontal">
                                           <asp:ListItem Value="1">Yes</asp:ListItem>
                                           <asp:ListItem Value="0">No</asp:ListItem>
                                       </asp:RadioButtonList>
                                   </ItemTemplate>
                               </asp:TemplateField>
                               <asp:TemplateField HeaderText="3">
                                   <ItemTemplate>
                                       <asp:RadioButtonList ID="RadioButtonList3" runat="server"
                                           RepeatDirection="Horizontal">
                                           <asp:ListItem Value="1">Yes</asp:ListItem>
                                           <asp:ListItem Value="0">No</asp:ListItem>
                                       </asp:RadioButtonList>
                                   </ItemTemplate>
                               </asp:TemplateField>
                               <asp:TemplateField HeaderText="4">
                                   <ItemTemplate>
                                       <asp:RadioButtonList ID="RadioButtonList4" runat="server"
                                           RepeatDirection="Horizontal">
                                           <asp:ListItem Value="1">Yes</asp:ListItem>
                                           <asp:ListItem Value="0">No</asp:ListItem>
                                       </asp:RadioButtonList>
                                   </ItemTemplate>
                               </asp:TemplateField>
                               <asp:TemplateField HeaderText="5">
                                   <ItemTemplate>
                                       <asp:RadioButtonList ID="RadioButtonList5" runat="server"
                                           RepeatDirection="Horizontal">
                                           <asp:ListItem Value="1">Yes</asp:ListItem>
                                           <asp:ListItem Value="0">No</asp:ListItem>
                                       </asp:RadioButtonList>
                                   </ItemTemplate>
                               </asp:TemplateField>
                               <asp:TemplateField HeaderText="Answer">
                                   <ItemTemplate>
                                       <asp:RadioButtonList ID="RadioButtonList6" runat="server"
                                           RepeatDirection="Horizontal">
                                           <asp:ListItem Value="0">a</asp:ListItem>
                                           <asp:ListItem Value="1">b</asp:ListItem>
                                           <asp:ListItem Value="2">c</asp:ListItem>
                                           <asp:ListItem Value="3">d</asp:ListItem>
                                           <asp:ListItem Value="4">e</asp:ListItem>
                                       </asp:RadioButtonList>
                                   </ItemTemplate>
                               </asp:TemplateField>
         
 
                </Columns>

                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                <HeaderStyle BackColor="White" Font-Bold="False" ForeColor="#000066" />
                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                <RowStyle BackColor="White" ForeColor="#003399" />
                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                <SortedDescendingHeaderStyle BackColor="#002876" />
            </asp:GridView>
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 40561343
Hi check the following code to access the controls in GridView. So that you can easily find the control which you want to hide.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Iterates through the rows of the GridView control
        For Each row As GridViewRow In GridView1.Rows
            ' Selects the text from the TextBox
            ' which is inside the GridView control
            Dim textBoxText As String = _
              CType(row.FindControl("TextBox1"),TextBox).Text
            Response.Write(textBoxText)
            ' Selects the text from the DropDownList
            ' which is inside the GridView control
            Dim dropDownListText As String = _
              CType(row.FindControl("DropDownList1"), _
              DropDownList).SelectedItem.Value
            Response.Write(dropDownListText)
            ' Selects items from the ListBox
            ' which is inside the GridView control 
            Dim myListBox As ListBox = _
                CType(row.FindControl("ListBox1"),ListBox)
            For Each selectedItem As ListItem In myListBox.Items
                ' Checks if the item in the ListBox is selected or not 
                If selectedItem.Selected Then
                    ' Print the value of the item if its selected
                    Response.Write(selectedItem.Value)
                End If
            Next
        Next
    End Sub

Open in new window


Check below URL for your reference.
http://www.codeproject.com/Articles/12021/Accessing-the-different-controls-inside-a-GridView
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40561362
If e.Row.RowIndex = 0 Then
                //e.Row.Cells(4).Visible = False
               // instead of hiding change its datasource/value to nothing
            End If
0
 

Author Closing Comment

by:murbro
ID: 40561426
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question