Solved

How to display just one line from datagrid child

Posted on 2004-09-29
22
230 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now