How do you sort member profile? And add multiple subheaders to a gridview?

Published Thursday, July 24, 2008 6:00 AM

membership, profileLife is what happens to you when you busy making other plans (by John Lehnon); In other words, life is full of surprises; in yet another words, there are always more variations than you  programmed for. Or changes are faster than plans.

Talk about programming...

I coded a small survey website with about 100 users, I used the asp .net membership and profile management system. The profile has the usual demographics elements: gender, race, income, etc.

Everything has been running smoothly and the site's admin has been happy. Then she asked me to sort and group all users by gender, then race, then income. Without even blinking, I said sure, thinking it could be done in 10 minutes.

I thought there must be a sort method within the profile system. But no. So I searched, not many people ever bothered with this. There was even one answer said flatly: forget it, profiles are not meant for sorting or grouping. To do this, please use customized profile system with database table.

But I do not want to throw my perfectly working profiles out of the window and start from anew.  So what do i do?

When I just about to write my own sort method, then I thought, what not simply populate key profile properties into a datatable, get a DataView, and use its sort method. Sure it will work.

Indeed. This is the code:

ProfileCommon profile;

            dt = new DataTable();

            dt.Columns.Add(new DataColumn("Race"));
            dt.Columns.Add(new DataColumn("Gender"));
            dt.Columns.Add(new DataColumn("Incomde"));
            dt.Columns.Add(new DataColumn("username"));
            dt.Columns.Add(new DataColumn("userkey"));

    //populate datatable
 foreach (MembershipUser m in allUsers)
            {

                userkey = m.ProviderUserKey.ToString();
                username = m.UserName;

                //categorize users by race and gender
                profile = Profile.GetProfile(username);

                DataRow r = dt.NewRow();
                r["username"] = username;
                r["Race"] = profile.Race;
                r["Gender"] = profile.Gender;
                r["userkey"] = userkey;
         r["Income"] = profile.Income;
          }

            //get the datatable default view
            DataView dv = dt.DefaultView;
            //sort
            dv.Sort = "Race desc, Gender desc, Income desc";

           //bind the view with a gridview
            gvProgress.DataSource = dv;
            gvProgress.DataBind();      

 Great.

 Now I had a second problem, how do I add the layers of subheaders to the display?

 It is not hard.

So I grabbed a piece of code from online and modified it slightly:

    private string[] m_subcategory = new string[] { "", "","" };
        private Color[] backcolors = new Color[] { Color.BlueViolet, Color.CornflowerBlue, Color.Cornsilk };
        private Color[] forecolors = new Color[] { Color.White, Color.BurlyWood, Color.Red };
        //Which column names we want to add subheader
        string[] fields = new string[] { "Completed", "Race", "Gender" };
    protected void gvRow_DataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = e.Row;
            switch (gvr.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        addSubHeader(fields, gvr);
                        break;
                    }
            }
       
        }
 private void addSubHeader(string[] fieldName, GridViewRow gvr)
        {

            for (int i = 0; i < fieldName.Length; i++)
            {
                DataRowView drv = gvr.DataItem as DataRowView;
                string subcategory = drv[fieldNameIdea].ToString();

                //string subSupplier = drv["supplierID"].ToString();
                //compare previous category with the current. 
                //if different, insert a new header
                if (!subcategory.Equals(m_subcategoryIdea))
                {
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow,
                      DataControlRowState.Normal);
                    TableCell cell = new TableCell();

                     cell.BackColor = backcolorsIdea;
                     cell.ForeColor = forecolorsIdea;
                    cell.ColumnSpan = gvProgress.Columns.Count;
                    cell.HorizontalAlign = HorizontalAlign.Left;

                    for (int j = 0; j < i; j++)
                        cell.Text += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    cell.Text += fieldNameIdea + " : " + subcategory;

                    row.Cells.Add(cell);
                    gvProgress.Controls[0].Controls.AddAt(gvProgress.Controls[0].Controls.Count - 1, row);
                    m_subcategoryIdea = subcategory;
                }

                //do not display text for the grouping cells
                gvr.CellsIdea.Text = "";
            }
        }

 


kick it on DotNetKicks.com

by xxxd
Filed under:

Comments

# DotNetKicks.com said on Thursday, July 24, 2008 10:29 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

This site

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating