Solved

Grouping rows in Gridview

Posted on 2009-05-07
5
1,228 Views
Last Modified: 2013-12-17
Hi Here's my code , but its not grouping the rows. I don't know why. please help me out in this. Actually i want the gridview to be grouped by manager. Here's the example.
HEADING1    HEADING2     HEADING3

MANAGER
Users            data                  datal
Users           data                  data
MANAGER


Here's my code. Please help me what's wrong with the code. thanks

Gridview.Databind()
GroupColumn(Gridview1,0);// 0- because i want the first column to be grouped.

public int GroupColumn(GridView dg, int ColumnIndex)
{
    int ItemIndex = 0;
    int Groupings = 0;
 
    foreach (GridViewRow dgi in dg.Rows)
    {
        if (dgi.RowIndex > 0)
        {
            //if current cells text is the same as the cell above it
            //make it invisible and increase the row span by 1 of the
            //last visible cell in that column.
            if (dgi.Cells[ColumnIndex].Text ==
                dg.Rows[dgItem.RowIndex-1].Cells[ColumnIndex].Text)
            {
                dgi.Cells[ColumnIndex].Visible = false;
                dg.Rows[ItemIndex].Cells[ColumnIndex].RowSpan =
                    dg.Rows[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
                Groupings++;
            }
            else if (dg.Rows[dgItem.RowIndex-1].Cells[ColumnIndex].Visible)
            {
                ItemIndex = dgi.RowIndex;
            }
            else
            {
                dg.Rows[ItemIndex].Cells[ColumnIndex].RowSpan =
                    dg.Rows[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
                ItemIndex = dgi.RowIndex;
            }
        }
    }
 
    dg.Rows[dg.Items.Count-1].Visible = false;
 
    return Groupings;
}

0
Comment
Question by:gladstonesheeba
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24332985
Hi gladstonesheeba,
Any reason not grouping the result in query level? That more easier to handle it.

Your result
==============
HEADING1    HEADING2     HEADING3

MANAGER
Users            data                  datal
Users           data                  data
MANAGER

Note: I've noticed that these are 2 different rows of records since the last data - "data1" and "data" is slightly different.



0
 

Author Comment

by:gladstonesheeba
ID: 24335262
I don't know how to group it by query level. all i have the sql query that has order by manager .  but that will just give  like this


manager  username
manager   username
manager   username
manager2  username2
manager2 username2


but i want it to be in the gridview like this . Hope you understood my question.

manager    
username
username
username
Manager2
username2
username2


How to do this in the query level?.
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 24351629
Hi gladstonesheeba,
Try check the complete guidelines from this resource for gridview grouping display:
http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm
0
 

Author Comment

by:gladstonesheeba
ID: 24355983
That's the one i already tried it , and it worked, but the only thing is i have a dropdownlist column in the gridview, if a user selects a value from the dropdown then the value should display in the textbox outside of the gridview. When i select a value from the dropdown , the page refreshes and an empty dropdownlist is created in that grouped row at first postback, the dropdown works only in the second postback. do you have any  idea on this?.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24392374
Hi gladstonesheeba,
Sorry for the delay reply. Can you post the related code snippet here? Let's us see how is the potential outcome for the applied based on requested.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the unique ID after insert? 87 90
WPF issue with Trigger 2 66
How to read XML file attributes... 17 40
crm development 2 36
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

14 Experts available now in Live!

Get 1:1 Help Now