Solved

ASP.net Make Cell Blank for certain rows

Posted on 2015-01-20
3
222 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
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 33

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now