Link to home
Start Free TrialLog in
Avatar of Mamine
Mamine

asked on

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 ?
Avatar of AerosSaga
AerosSaga

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
Avatar of Mamine

ASKER

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") ... ?
Avatar of Mamine

ASKER

Yep bin there, seen that and tried it ... still doesn't get to the hierarchical prob tho; :(
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?

Avatar of Mamine

ASKER

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)
THe 1st child is orderd by what db field?
Avatar of Mamine

ASKER

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)
Avatar of Mamine

ASKER

er the child is related to the parent by ProductID
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.
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)
Avatar of Mamine

ASKER

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
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
Avatar of Mamine

ASKER

Oh I see ... the dataset is already sorted ... I just want to pick out the top element i.e. DataItem(0)
Avatar of Mamine

ASKER

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 ?
Avatar of Mamine

ASKER

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.
glad to hear you got it worked out Mamine

Aeros
Avatar of Mamine

ASKER

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

Aeros
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial