Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

Grouping rows in Gridview

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
gladstonesheeba
Asked:
gladstonesheeba
  • 3
  • 2
1 Solution
 
David H.H.LeeCommented:
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
 
gladstonesheebaAuthor Commented:
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
 
David H.H.LeeCommented:
Hi gladstonesheeba,
Try check the complete guidelines from this resource for gridview grouping display:
http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm
0
 
gladstonesheebaAuthor Commented:
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
 
David H.H.LeeCommented:
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now