?
Solved

How to display just one line from datagrid child

Posted on 2004-09-29
22
Medium Priority
?
243 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
[X]
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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

765 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