tags:

views:

626

answers:

3

Hi,

I'm building a GridView control which encapsulates a child gridview. The child gridview holds a div tag which is displayed when the user selects a row in the parent gridview. However, even though the contents is hidden i.e. the div tag, an extra column is added - how do I get rid of the extra column. In the tutorial it states that by adding a </td></td> and starting a new row <tr> this should happen but it does (I also noticed that the author turned off gridlines so my assumption is that he in fact has this problem also). Here is the gridview, oh and I set the visible state of the itemtemplate to 'true' but then the javascript could (not) find it.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
  AutoGenerateColumns="False" DataKeyNames="PublicationID" 
  DataSourceID="ObjectDataSource1" Width="467px" OnRowDataBound="GridView1_RowDataBound"
  Font-Names="Verdana" Font-Size="Small">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="PublicationSelector" runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
    <asp:TemplateField HeaderText="Owners">
      <ItemTemplate>
       <asp:Label ID="Owners" runat="server"></asp:Label>
      </ItemTemplate>
      <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
    <asp:TemplateField>
      <ItemTemplate >
      </td></tr>
        <tr>
          <td colspan="7">
            <div id="<%# Eval("PublicationID") %>" style="display: none; position: relative">
              <asp:GridView ID="GridView2_ABPubs" runat="server" AutoGenerateColumns="false" Width="100%"
                Font-Names="Verdana" Font-Size="small">
                <Columns>
                  <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
                </Columns>
              </asp:GridView>
            </div>
          </td>
        </tr>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

Apart from the extra column in the master gridview it works fine.

Just for completeness, here is a to the original article (for some reason it didn't like my <a href> tag so its copy and paste).

Thanks.

A: 

Looks like you have unbalanced tags in your <ItemTemplate>:

<ItemTemplate >
  </td></tr> <<---- These look unbalanced
    <tr>
      <td colspan="7">
        <div id="<%# Eval("PublicationID") %>" style="display: none; position: relative">
          <asp:GridView ID="GridView2_ABPubs" runat="server" AutoGenerateColumns="false" Width="100%"
            Font-Names="Verdana" Font-Size="small">
            <Columns>
              <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
            </Columns>
          </asp:GridView>
        </div>
      </td>
    </tr>
  </ItemTemplate>
Keltex
A: 

I don't see an opening tag for these tr, td tags:

...
<ItemTemplate >
      </td></tr>
...

Just checked and the author of the article seems to have the same issue in the generated source of the page.

Cerebrus
+1  A: 

To get rid of the extra column, just set its css style to display: none. You can do this by applying a CssClass to the TemplateField containing the nested grid:

<asp:TemplateField HeaderStyle-CssClass="hidden-column" ItemStyle-CssClass="hidden-column" FooterStyle-CssClass="hidden-column">

Here is the definition of the CssClass I used:

<style type="text/css">
  .hidden-column {
    display: none;
  }
</style>

Note: the markup will still be in the html but at least it won't be visible.
Tested under IE 8.0, Google Chrome 2.0 and Opera 10.0

Update: To eliminate the double border, just put the id and the style on the <tr> instead of the <div>:

<tr id="<%# Eval("PublicationID") %>" style="display: none; position: relative">
  <td colspan="7">
    <div>
...

... and change the display in the javascript from block to table-row:

div.style.display = "table-row";  // not a div anymore!!
Julien Poulin
Ok, I was experimenting with 0 height rows but this just blew me away. Awesome answer, thank you.
flavour404
My only quibble, and this is a small one, is that I still have a what looks like a double border at the bottom of each row where the template field and my hidden div is. Any suggestion about how to get rid of that, or set it to 1px would be preferable?Thanks again.
flavour404
Hi, not a quibble but in Opera it removes the top border of the parent <tr> after the row is clicked and the div displayed. If you hide it again, the top border remains invisible but if you refresh the page it appears.Thanks, R.
flavour404
And the other thing I have noticed is that if you click on one of the other rows which does not have a child grid a 1px row appears.Thanks.
flavour404
I haven't looked at the problem in Opera but for the rows that don't have child data, you should use the EmptyDataText property of the GridView to make it more obvious to the users that there is no data present.
Julien Poulin