Unit test for Sorting & Filtering - RadGridView

2 posts, 0 answers
  1. Karandeep Singh
    Karandeep Singh avatar
    4 posts
    Member since:
    May 2010

    Posted 24 Apr 2012 Link to this post


    I want to write automated unit tests for Sort and filter functionality in RadGridView. 
    In my test method I want to bind 1000 mock records to the GridView and run the sort and filter. 
    Is that possible? If Yes throw some light please.

    Karandeep Singh
  2. Julian Benkov
    Julian Benkov avatar
    1135 posts

    Posted 27 Apr 2012 Link to this post

    Hi Karandeep,

    Here is a sample code with unit tests for the filtering and sorting operations:

    public void TestLikeOperatorWithWildcard()
        using (Form form = new Form())
            form.Size = new System.Drawing.Size(640, 480);
            RadGridView grid = new RadGridView();
            grid.Dock = System.Windows.Forms.DockStyle.Fill;
            grid.EnableFiltering = true;
            DataTable table = new DataTable("table");
            table.Columns.Add("Value", typeof(byte));
            grid.DataSource = table;
            grid.Columns[1].HeaderText = "Test3";
            grid.Columns[1].Name = "Test3";
            grid.Rows.Add("111", "Nancy Davolio", "Seattle", 12);
            grid.Rows.Add("222", "Andrew Fuller", "Tacoma", 4);
            grid.Rows.Add("333", "Janet Leverling", "Kirkland", 6);
            grid.Rows.Add("444", "Margaret \"Peacock", "Redmond", 8);
            grid.Rows.Add("555", "Steven Buchanan", "London", 2);
            //apply filter descriptor with LIKE operator
            grid.FilterDescriptors.Add(new FilterDescriptor("City", FilterOperator.IsLike, "%ond"));
            Assert.AreEqual(grid.MasterView.ChildRows.Count, 1, "Wrong filtered rows number with LIKE operator");
            //apply filter descriptor with NOT LIKE operator
            grid.FilterDescriptors.Add(new FilterDescriptor("City", FilterOperator.IsNotLike, "Londo%"));
            Assert.AreEqual(grid.MasterView.ChildRows.Count, 4, "Wrong filtered rows number with NOT LIKE operator");
    public void RowIndexAfterSortingTest()
        using (Form form = new Form())
            RadGridView gridView = UnitTestHelper.AttachGrid(form);
            gridView.ColumnCount = 2;
            gridView.Rows.Add("Ivan", 1);
            gridView.Rows.Add("Petur", 1);
            gridView.Rows.Add("Dragomir", 1);
            gridView.Rows.Add("Veselin", 2);
            gridView.Rows.Add("Kocho", 2);
            gridView.Rows.Add("Gocho", 3);
            gridView.Rows.Add("Tashko", 3);
            gridView.SortDescriptors.Expression = "column2 desc";
            Assert.AreEqual("Gocho", gridView.ChildRows[0].Cells[0].Value);
            Assert.AreEqual(0, gridView.ChildRows[0].Index);
            Assert.AreEqual("Tashko", gridView.ChildRows[1].Cells[0].Value);
            Assert.AreEqual(1, gridView.ChildRows[1].Index);
            Assert.AreEqual("Veselin", gridView.ChildRows[2].Cells[0].Value);
            Assert.AreEqual(2, gridView.ChildRows[2].Index);
            Assert.AreEqual("Kocho", gridView.ChildRows[3].Cells[0].Value);
            Assert.AreEqual(3, gridView.ChildRows[3].Index);
            Assert.AreEqual("Ivan", gridView.ChildRows[4].Cells[0].Value);
            Assert.AreEqual(4, gridView.ChildRows[4].Index);
            Assert.AreEqual("Petur", gridView.ChildRows[5].Cells[0].Value);
            Assert.AreEqual(5, gridView.ChildRows[5].Index);
            Assert.AreEqual("Dragomir", gridView.ChildRows[6].Cells[0].Value);
            Assert.AreEqual(6, gridView.ChildRows[6].Index);

    I hope this helps.

    Julian Benkov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. DevCraft banner
Back to Top