Hide rows in GridView with duplicate column data

3 posts, 0 answers
  1. Braden
    Braden avatar
    37 posts
    Member since:
    Aug 2018

    Posted 15 Aug 2018 Link to this post

    Hello everyone,

    Title basically explains the problem. I have some code that I tried searching for online, but it doesn't do what I want it to.

    See screenshot to see the duplicate data I'm trying to hide (i.e. hide the second row -- id: 34 -- where the business names are both "Tractor Supply Warehouse"

    Here's the code I have now, though I've tried a few different approaches with no success. What happens in this one is that the value of the duplicateIndexList never changes...so everything after the duplicate Tractor Supply Warehouse row (id: 34) gets hidden, which I don't want!

    Thanks!

    private void MPLListRowCreation()
            {
                MPLList_GridView.Rows.Clear();
                this.MPLList_GridView.BeginUpdate();
                List<int> duplicateIndexList = new List<int>();
     
                foreach (BPPriceMstr myMpl in masterPriceLists)
                {
                    int[] dupl;
                    GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(this.MPLList_GridView.MasterView);
                    int count = 0;
                    rowInfo.Cells[0].Value = Convert.ToInt32(myMpl.Id);
                    rowInfo.Cells[1].Value = myMpl.Business.CardName;
                    rowInfo.Cells[2].Value = myMpl.StartDate.ToString("MM/dd/yyyy");
                    rowInfo.Cells[3].Value = myMpl.EndDate.ToString("MM/dd/yyyy");
                    rowInfo.Cells[4].Value = myMpl.CreatedBy;
                    rowInfo.Cells[5].Value = myMpl.CreatedOn.ToString("MM/dd/yyyy");
                    rowInfo.Cells[6].Value = myMpl.ModifiedBy;
                    if (myMpl.ModifiedOn.ToString("MM/dd/yyyy") != "01/01/0001")
                    {
                        rowInfo.Cells[7].Value = myMpl.ModifiedOn.ToString("MM/dd/yyyy");
                    }
                    rowInfo.Cells[8].Value = myMpl.TermsPackage.PackageName;
     
                    //set our search paramater (the row/cardname we're currently on)
                    //iterate over all of the MPL objects and see if there is more than one match...
                    string searchedStr = myMpl.Business.CardName;
                    for (int i=0; i<masterPriceLists.Count; i++)
                    {
                        if (masterPriceLists[i].Business.CardName.ToUpper().Equals(searchedStr.ToUpper()))
                        {
                            count++;
                            if (count > 1)
                            {
                                //if it finds more than one MPL, add the duplicate indices to an array
                                duplicateIndexList.Add(myMpl.Id);
                            }
                        }
                    }
                     
                    //if we have duplicates...
                    if (duplicateIndexList.Count != 0)
                    {
                        dupl = duplicateIndexList.ToArray();
                        int firstEntry = dupl[0];
                        //hide all indices greater than the first entry... i.e. hide all indexes greater than the first one in the array
                        foreach (var item in dupl)
                        {
                            if (firstEntry.ToString().Equals(item.ToString()))
                            {
                                rowInfo.IsVisible = true;
                                //here we also probably want to show that there are multiple start/end dates for that MPL
                                //rowInfo.Cells[2].Value = "Multiple";
                                //rowInfo.Cells[3].Value = "Multiple";
                            }
                            else
                            {
                                rowInfo.IsVisible = false;
                            }
                        }
                    }
                    MPLList_GridView.Rows.Add(rowInfo);
                }
                this.MPLList_GridView.EndUpdate();
            }
  2. Braden
    Braden avatar
    37 posts
    Member since:
    Aug 2018

    Posted 16 Aug 2018 Link to this post

    I figured it out after searching online for a bit...here's my implementation: 

    private void MPLListRowCreation()
            {
                MPLList_GridView.Rows.Clear();
                this.MPLList_GridView.BeginUpdate();
     
                foreach (BPPriceMstr myMpl in masterPriceLists)
                {
                    GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(this.MPLList_GridView.MasterView);
                    rowInfo.Cells[0].Value = Convert.ToInt32(myMpl.Id);
                    rowInfo.Cells[1].Value = myMpl.Business.CardName;
                    rowInfo.Cells[2].Value = myMpl.StartDate.ToString("MM/dd/yyyy");
                    rowInfo.Cells[3].Value = myMpl.EndDate.ToString("MM/dd/yyyy");
                    rowInfo.Cells[4].Value = myMpl.CreatedBy;
                    rowInfo.Cells[5].Value = myMpl.CreatedOn.ToString("MM/dd/yyyy");
                    rowInfo.Cells[6].Value = myMpl.ModifiedBy;
                    if (myMpl.ModifiedOn.ToString("MM/dd/yyyy") != "01/01/0001")
                    {
                        rowInfo.Cells[7].Value = myMpl.ModifiedOn.ToString("MM/dd/yyyy");
                    }
                    rowInfo.Cells[8].Value = myMpl.TermsPackage.PackageName;
     
                     
                    MPLList_GridView.Rows.Add(rowInfo);
                }
                //check for duplicates here, before ending the MPL browser view update
                generateUniqueData();
                this.MPLList_GridView.EndUpdate();
            }
    private void generateUniqueData()
           {
               List<string> list = new List<string>();
               for(int i=0; i<MPLList_GridView.Rows.Count; i++)
               {
                   string str = MPLList_GridView.Rows[i].Cells[1].Value.ToString();
                   if (!list.Contains(str))
                   {
                       list.Add(str);
                   }
                   else
                   {
                       MPLList_GridView.Rows.Remove(MPLList_GridView.Rows[i]);
                   }
               }
     
           }
  3. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3575 posts

    Posted 17 Aug 2018 Link to this post

    Hello, Braden,      

    I am glad that you have a suitable solution for your case. Have in mind that you can easily hide rows by manipulating the IsVisible property of the respective row. 
    this.radGridView1.Rows[3].IsVisible = false;

    I hope this information helps. If you have any additional questions, please let me know.  
     
    Regards,
    Dess
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top