Hide duplicate values in RadGrid column

7 posts, 0 answers
  1. Hanoch Faran
    Hanoch Faran avatar
    7 posts
    Member since:
    Aug 2008

    Posted 15 Jan 2009 Link to this post

    I want to bind the grid to query results.
    A person might appear several times and I want to sort the results using person ID.
    Is there a way to hide duplicate values (e.g. ID, First name, Last name etc.) in the grid?

    I wish there was a property like 'HideDuplicates'  in MS Access.

  2. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 15 Jan 2009 Link to this post

    Hello Hanoch,

    You can implement this functionality on database level. For example:

    SELECT DISTINCT ID, FirstName, LastName FROM YourTable

    Let us know if you need more information.

    Best regards,
    Daniel
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Hanoch Faran
    Hanoch Faran avatar
    7 posts
    Member since:
    Aug 2008

    Posted 15 Jan 2009 Link to this post

    Thanks Daniel

    But a DISTINCT won't help here since some columns will be different (such as order id, order date etc.)
    I want the grid to look like in this example:
    Man ID | Name    | Order ID | Order Date | Total Price
    =========================================
    |  101   | Joe K.   | 1001       | 1/1/09         | 50$  
    |           |              | 1004       | 3/1/09         | 120$  
    |  103   | Tim B.   | 1002       | 1/1/09         | 70$  
    |           |              | 1003       | 4/1/09         | 100$  

    As I said, a new GridBoundColumn's property called HideDuplicates will be nice.

    Meanwhile, I managed to get what I wanted with this method:

    1) I wrote this generic function:
            private Hashtable hashLastValues = new Hashtable();
    ...
            protected string myHideDuplicate(object oField,string FldName)
            {
                string CurValue = oField.ToString();
                if (hashLastValues.ContainsKey(FldName))
                    if ((string)hashLastValues[FldName]==CurValue)
                        CurValue = "";
                    else
                        hashLastValues[FldName]=CurValue;
                else
                    hashLastValues.Add(FldName, CurValue);

                return CurValue;
            }

    2) I defined columns in the grid like this:
                <telerik:GridTemplateColumn DataField="ManFamily" HeaderText="Last Name">
                <ItemTemplate>
                <asp:Label runat="server" Text='<%# myHideDuplicate(Eval("ManFamily"),"ManFamily") %>'></asp:Label>
                </ItemTemplate>
                </telerik:GridTemplateColumn>

    And it's working fine now.
  4. IT
    IT avatar
    26 posts
    Member since:
    Dec 2008

    Posted 21 Jan 2010 Link to this post

    Hi here

    I've also been looking into doing the same thing

    but no success

    how does your method work ?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Jan 2010 Link to this post

    Hello Brian,

    You can merge the cells having duplicate values using the following code:
    c#:
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
           for (int rowIndex = RadGrid1.Items.Count - 2; rowIndex >= 0; rowIndex--) 
            { 
                GridDataItem row = RadGrid1.Items[rowIndex]; 
                GridDataItem previousRow = RadGrid1.Items[rowIndex + 1]; 
     
                for (int i = 0; i < row.Cells.Count; i++) 
                { 
                    if (row.Cells[i].Text == previousRow.Cells[i].Text) 
                    { 
                        row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1; 
                        previousRow.Cells[i].Visible = false
                    } 
                } 
     
            } 
       } 

    Hope this helps..
    Princy.
  6. Karthikeyan
    Karthikeyan avatar
    27 posts
    Member since:
    Aug 2011

    Posted 19 Sep 2011 Link to this post

    Hi Princy,

    Will it applicable for Silverlight RADGRIDVIEW also.
    If yes, Can you please tell me how we can call this method/changes?

    Please guide me to do.

    Thanks,
    Karthikeyan Manickam.
  7. Mark
    Mark avatar
    2 posts
    Member since:
    Apr 2012

    Posted 24 Oct 2018 in reply to Hanoch Faran Link to this post

    This solution is perfect, worked for me. Thanks Hanoch Faran.
Back to Top