Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Hide duplicate row values in a Gridview Control

Controls

How do I Hide duplicate row values in a Gridview Control

by  Chance1234  Posted    (Edited  )
Say you have the following list of data

Country, Town, Sales
UK, London, 4322
UK, Leeds, 2342
UK, Salisbury, 1234
USA,New York, 1555
USA,Washington,1200
USA,Texas, 1112

On your Gridview, you want to show the data as

UK London 4322
Leeds 2342
Salisbury 1234
USA New York 1555
Washington 1200
Texas 1112


To hide the duplicate values (in this case country) use the following code on the RowDataBound event.

Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //switch for first row 
            if (e.Row.RowIndex == 1)
            {
                GridViewRow Gprev = GridView1.Rows[e.Row.RowIndex - 1];
                if (Gprev.Cells[0].Text.Equals(e.Row.Cells[0].Text))
                {
                    e.Row.Cells[0].Text = "";
                }
            }
            //now continue with the rest of the rows

            if (e.Row.RowIndex > 1)
            {
                //set reference to the row index and the current value
                int intC = e.Row.RowIndex;
                string lookfor = e.Row.Cells[0].Text;

                //now loop back through checking previous entries for matches 
                do
                {
                   GridViewRow Gprev = GridView1.Rows[intC-1];

                    if (Gprev.Cells[0].Text.Equals(e.Row.Cells[0].Text))
                        {
                            e.Row.Cells[0].Text = "";
                        }

                  intC = intC - 1;
                } while (intC >0);

            }

        }

    }


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top