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
Solved

How to display just one line from datagrid child

Posted on 2004-09-29
22
239 Views
Last Modified: 2012-06-27
I have a dataset which contains 2 tables in hierarchical form (Products with Shops which sell them). I am displaying them in a datagrid so that the rows show the Products and a special templated column shows the related Shops (in another datagrid) which can be visible/hidden at the press of a button.

All that is fine and dandy but what I really want to do is show the 1st row of the Shops table (for each Product) even if the button is set to hide the details.

I have set up 2 divs so that the button can alternate between them, but I can't find the right format to access the data in the 1st row of the Shops table to fill, for example, a label with the right data. Have tried Container.DataItem.Row.GetChildRows("myRelationship").Item(0).Item(0) and other similar but can't get a format that works.

If I use another embedded datagrid/datalist then I can use Container.DataItem.Row.GetChildRows("myRelationship") as the source but I can't find a way to limit the number of rows displayed to only one.

Can anyone help or suggest an alternative method ?
0
Comment
Question by:Mamine
  • 10
  • 10
22 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12181968
You could use javascript:

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  'Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))


  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      BindData()
    End If
  End Sub

      
  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT * FROM tblFAQ WHERE FAQID <= 20"

    'Set the datagrid's datasource to the datareader and databind
    Dim resultsDataSet as New DataSet()
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
    myDataAdapter.Fill(resultsDataSet)  

    rptFAQs.DataSource = resultsDataSet
    rptFAQs.DataBind()
  End Sub
</script>

<script language="JavaScript">
  function ToggleDisplay(id)
  {
    var elem = document.getElementById('d' + id);
    if (elem)
    {
      if (elem.style.display != 'block')
      {
        elem.style.display = 'block';
        elem.style.visibility = 'visible';
      }
      else
      {
        elem.style.display = 'none';
        elem.style.visibility = 'hidden';
      }
    }
  }
</script>

<style>
    .header { font-size: larger; font-weight: bold; cursor: hand; cursor:pointer;
               background-color:#cccccc; font-family: Verdana; }
    .details { display:none; visibility:hidden; background-color:#eeeeee;
               font-family: Verdana; }
</style>

<asp:Repeater id="rptFAQs" runat="server">
   <ItemTemplate>
     <div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header"
          onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
       <%# DataBinder.Eval(Container.DataItem, "Description") %>
     </div>
         
     <div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
       <b>Submitted By:</b> <%# DataBinder.Eval(Container.DataItem, "SubmittedByName") %><br />
       <b>Views:</b> <%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:d}") %><br />
       <b>FAQ:</b><br />
       <%# DataBinder.Eval(Container.DataItem, "Answer") %>
     </div>
   </ItemTemplate>
</asp:Repeater>
      

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12181976
a  great article on this methodology:

http://aspnet.4guysfromrolla.com/articles/091504-1.aspx
0
 

Author Comment

by:Mamine
ID: 12182001
As far as I can tell I already have the equivalent of all that Aeros ... the bit I'm missing is how to get at the 1st row of the hieararchical datatable i.e. where you've got DataBinder.Eval(Container.DataItem,"whatever"), I need to put something like DataBinder.Eval(Container.DataItem.GetChildRows.DataItem(0),"whatever") ... ?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Mamine
ID: 12182014
Yep bin there, seen that and tried it ... still doesn't get to the hierarchical prob tho; :(
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12182088
I'm not sure what your asking then.  You said you always want the parent visible regardless of whether the child is visible or not correct?  Or have I misunderstood you?

0
 

Author Comment

by:Mamine
ID: 12182540
The parent is always visible anyway, thats no prob. I already have a child datagrid to display details if user presses a button. What I'm missing is the ability to display the 1st child ONLY (if the button is not pressed or details not shown)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12182562
THe 1st child is orderd by what db field?
0
 

Author Comment

by:Mamine
ID: 12182563
i.e. either how to get a datagrid/datalist to display only the first line (without paging)

or ...

how to get the data from the 1st child (using the DataBinder.Eval argument)
0
 

Author Comment

by:Mamine
ID: 12182574
er the child is related to the parent by ProductID
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12182587
how to get the data from the 1st child (using the DataBinder.Eval argument)

you might be able to get that one based on sql min(x) if your ordering by ID or someother numeric field.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12182634
then use sql like I showed you to get the min or max product id and databind the the others to a collasable region

Aggregate functions in SQL Server
Function       Description
AVG(column)       Returns the average value of a column
BINARY_CHECKSUM       
CHECKSUM       
CHECKSUM_AGG       
COUNT(column)       Returns the number of rows (without a NULL value) of a column
COUNT(*)       Returns the number of selected rows
COUNT(DISTINCT column)       Returns the number of distinct results
FIRST(column)       Returns the value of the first record in the specified field (not supported in SQLServer2K)
LAST(column)       Returns the value of the last record in the specified field (not supported in SQLServer2K)
MAX(column)       Returns the highest value of a column
MIN(column)       Returns the lowest value of a column
STDEV(column)       
STDEVP(column)       
SUM(column)       Returns the total sum of a column
VAR(column)       
VARP(column)
0
 

Author Comment

by:Mamine
ID: 12182637
hmmm I could do it at the SQL end but the sub-query is a pain in the butt on performance, hence I've used this method of having a related child table in the dataset. The problem here isn't the ordering, its just being able to access one and only one row from within the dataset from html/asp.net
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12182656
I understand that but you have to exploit some means of determining the first/last child.  I offer you Min/Max.

If ms access then you could use first last
0
 

Author Comment

by:Mamine
ID: 12182670
Oh I see ... the dataset is already sorted ... I just want to pick out the top element i.e. DataItem(0)
0
 

Author Comment

by:Mamine
ID: 12182746
I'm obviously not explaining this well. Let me have another go .... I want to do something like:

<div id='<%# DataBinder.Eval(Container, "DataItem(0)") %>collapsed' style="DISPLAY: block; VISIBILITY: visible">
<%# DataBinder.Eval(*****the top row from the GetChildRows rows ******)%>
</div>

the div is fine, no prob but I cant get the syntax right to display the Shop Name.

This would give me the Product (i.e. Parent) ID but I need the Shop (child)

<%# DataBinder.Eval(Container, "DataItem(0)") %>

so I reckoned on something like:

<%# DataBinder.Eval(Container.DataItem.GetChildRows("myRelation").Item(0), "DataItem(0)") %>

but I just get errors saying that Item is not a member of Rows()

Does this help explain what I mean ?
0
 

Author Comment

by:Mamine
ID: 12187855
If anyone else is interested in this problem, I've solved it by taking the child values directly, rather than using DataBinder.Eval().

The syntax is:

Container.DataItem.Row.GetChildRows("myRelationship")(0).Item(x)

where x is the number of the column that you want to reference.

I still haven't figured out the DataBinder.Eval syntax, but I can live without that.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12188963
glad to hear you got it worked out Mamine

Aeros
0
 

Author Comment

by:Mamine
ID: 12188976
hehe thx Aeros :)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12188986
yw ;)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12192821
The asker solved her own question recomendation  >>>PAQ/Refund

Aeros
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12222327
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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