How to display just one line from datagrid child

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 ?
MamineAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ee_ai_constructConnect With a Mentor Commented:
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0
 
AerosSagaCommented:
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
 
AerosSagaCommented:
a  great article on this methodology:

http://aspnet.4guysfromrolla.com/articles/091504-1.aspx
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
MamineAuthor Commented:
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
 
MamineAuthor Commented:
Yep bin there, seen that and tried it ... still doesn't get to the hierarchical prob tho; :(
0
 
AerosSagaCommented:
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
 
MamineAuthor Commented:
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
 
AerosSagaCommented:
THe 1st child is orderd by what db field?
0
 
MamineAuthor Commented:
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
 
MamineAuthor Commented:
er the child is related to the parent by ProductID
0
 
AerosSagaCommented:
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
 
AerosSagaCommented:
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
 
MamineAuthor Commented:
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
 
AerosSagaCommented:
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
 
MamineAuthor Commented:
Oh I see ... the dataset is already sorted ... I just want to pick out the top element i.e. DataItem(0)
0
 
MamineAuthor Commented:
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
 
MamineAuthor Commented:
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
 
AerosSagaCommented:
glad to hear you got it worked out Mamine

Aeros
0
 
MamineAuthor Commented:
hehe thx Aeros :)
0
 
AerosSagaCommented:
yw ;)
0
 
AerosSagaCommented:
The asker solved her own question recomendation  >>>PAQ/Refund

Aeros
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.