Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group By On Datagrid

Status
Not open for further replies.

doubletalkin3

Programmer
Joined
Apr 24, 2006
Messages
6
Location
GB
Hi,

I'm trying to create item headings on a contacts datagrid to group it by Department. For example -

Accounts
Person A - Phone Number - Email etc
Person B - Phone Number - Email etc
Person C - Phone Number - Email etc
Sales
Person A - Phone Number - Email etc
Person B - Phone Number - Email etc
Person C - Phone Number - Email etc

Looking around the internet, I've found lots of summary rows, which calculates totals, sub totals, and groupings by date and integers. I've also seen this done through a data repeater, but I would like the department name to be displayed in a row of the datagrid.

I've put the group by clause in the SQL statement.

I think the code would be put in the ItemDataBound function and determine when the department name changes - insert a summary row displaying the department name as text (As a lot of people here query the database directly, we store the department name in the contacts table, not the ID)

Any ideas what the ItemDataBound code would look like for a string? Thanks
 
One method would be to use Nested DataGrids. There are quite a few articles on this method and here's one I found from google:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
this "automatic" datalist grouping is kinda nifty...

visit to see it in action (the links on the left is what this is)

HTML
Code:
<asp:DataList CssClass="dgWallS" ID="dlWallS" runat="server" OnItemDataBound="link_idb">
    <HeaderTemplate>
        <table class="policy" border="0" cellpadding="0" cellspacing="0" bgcolor="#d8d8d8"
            background="../img/nav_fill.jpg">
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td width="180">
                <asp:Label CssClass="navHead" ID="lblTitle" Width="180" runat="server" Text='<%#Container.DataItem("linkCategory")%>'></asp:Label>
                <li>
                    <asp:HyperLink ID="HyperLink1" ToolTip='<%#Container.DataItem("linkDesc")%>' runat="server"
                        NavigateUrl='<%#Container.DataItem("linkURL")%>' class="navLeft" Text='<%#Container.DataItem("linkName")%>'
                        Target='<%# checkTarget(Container.DataItem("linkTarget"))%>' />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:DataList>

Code
Code:
    Sub link_idb(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim strval As String = CType(e.Item.FindControl("lblTitle"), Label).Text
            Dim title As String = ViewState("title")
            If title = strval Then
                CType(e.Item.FindControl("lblTitle"), Label).Visible = False
            Else
                title = strval
                ViewState("title") = title
                CType(e.Item.FindControl("lblTitle"), Label).Text = title
                e.Item.Visible = True
            End If
        End If
    End Sub
 
Thanks for the help guys - that nested datagrid may prove to be very useful for something else I'm working on, but what I really need for this is for the department name to appear as a row spanned across the columns, then the resulting fields to appear underneath in the datagrid. Any ideas on how I can achieve this
 
You could probably nest a datagrid inside a repeater (as you coan then format the repeater to look exactly how you want it e.g. as a row).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top