Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Grouping rows in Gridview

Posted on 2009-05-07
5
Medium Priority
?
1,236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

705 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