I’m pleased to announce that with Q1 2009 release of RadControls for WPF / Silverlight you will be able to manipulate the controls very easily with two powerful extension methods: ParentOfType<> and ChildrenOfType<>.

Here are several small demos for RadGridView:

1) Get all grid rows:
     var rows = RadGridView1.ChildrenOfType<GridViewRow>();


2) Get all grid cells:
     var cells= RadGridView1.ChildrenOfType<GridViewCell>();


3) Get all grid header cells:
     var headerCells = RadGridView1.ChildrenOfType<GridViewHeaderCell>();


4) Get (and show) new row:
     var newRow = RadGridView1.ChildrenOfType<GridViewNewRow>().First();
     newRow.Visibility = Visibility.Visible;


5) Find the grid vertical scrollbar and scroll the grid to bottom:
    var verticalScrollBar = RadGridView1.ChildrenOfType<ScrollBar>().Where(s=>s.Orientation == Orientation.Vertical).First();
    verticalScrollBar.Value = verticalScrollBar.Maximum;


6) Select specific row(s):
    var row = RadGridView1.ChildrenOfType<GridViewRow>().Where(r => r.ChildrenOfType<GridViewCell>().Where(c => c.Content.ToString() == "ALFKI").Any()).First();
    row.IsSelected = true;


7) Change specific cell(s) content:
    var cell = RadGridView1.ChildrenOfType<GridViewCell>().Where(c => c.Content.ToString() == "ALFKI").First();
    cell.Content = "Changed!";


8) Put specific cell in edit mode:
    var cell = RadGridView1.ChildrenOfType<GridViewCell>().Where(c => c.Content.ToString() == "ALFKI").First();
    cell.IsInEditMode = true;


9) Get visible rows count on scroll or size change:
    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        var verticalScrollBar = RadGridView1.ChildrenOfType<ScrollBar>().Where(s => s.Orientation == Orientation.Vertical).First();
        verticalScrollBar.SizeChanged += new SizeChangedEventHandler(verticalScrollBar_SizeChanged);
        verticalScrollBar.Scroll += new ScrollEventHandler(verticalScrollBar_Scroll);
    }

    void verticalScrollBar_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        var visibleRowsCount = RadGridView1.ChildrenOfType<GridViewRow>().Where(r => r.Visibility == Visibility.Visible).Count();
    }

    void verticalScrollBar_Scroll(object sender, ScrollEventArgs e)
    {
        var visibleRowsCount = RadGridView1.ChildrenOfType<GridViewRow>().Where(r => r.Visibility == Visibility.Visible).Count();
    }


10) Perform conditional row(s) formatting:
    var rows = RadGridView1.ChildrenOfType<GridViewRow>().Where(r => r.ChildrenOfType<GridViewCell>().Where(c => c.Content.ToString() == "ALFKI"|| c.Content.ToString() == "AROUT").Any()).ToList();
    rows.ForEach(r => r.Background = Brushes.Red);

11) Expand first row in case of hierarchy:
    var expandableRow = RadGridView1.ChildrenOfType<GridViewExpandableRow>().First();
    expandableRow.IsExpanded = true;

12) Collapse first group header row:
   var groupHeader = RadGridView1.ChildrenOfType<GridViewGroupRow>().First();
   groupHeader.ChildrenOfType<Expander>().First().IsExpanded = false;

13) Modify background for all cells in specific column:
   var unitPriceCells = RadGridView1.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == "UnitPrice").ToList();
   unitPriceCells.ForEach(c => c.Background = Brushes.Orange);

14) Increase header row Height:
   var headerRow = RadGridView1.ChildrenOfType<GridViewHeaderRow>().First();
   headerRow.Height = 100;


15) Check all CheckBox controls in specific column:
   var cells = RadGridView1.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == "Test").ToList();
   cells.ForEach(c => c.ChildrenOfType<CheckBox>().First().IsChecked = true);

Enjoy!


About the Author

Vladimir Enchev

is Director of Engineering, Native Mobile UI & Frameworks

Comments

Comments are disabled in preview mode.