Solved

Grouping rows in Gridview

Posted on 2009-05-07
5
1,229 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now