Accessing of rows in Silverlight DataGrid

Source: Internet
Author: User

Imagine you want to enumerate (enlist) All rows (datagridrow) of Silverlight grid (DataGrid). By design this is not very simple tasks.

For example, you want to do something like this:

Foreach (maid in grid. Rows)
{
...
}

This very important and very frequent requirement is just an issue. you will notice that this is almost impossible and will start to research in Internet. good luck. so, I decided to post the code of extension class which makes this possible:

Foreach (maid ())
{
...
}

Here is the whole code:

/// <Summary>
/// Extends the DataGrid.
/// </Summary>
Public static class datagridextensions
{
/// <Summary>
/// Gets the list of datagridrow objects.
/// </Summary>
/// <Param name = "Grid"> the grid wirhrows. </param>
/// <Returns> List of rows of the grid. </returns>
Public static icollection <datagridrow> getrows (this DataGrid grid)
{
List <datagridrow> rows = new list <datagridrow> ();

Foreach (VAR rowitem in grid. itemssource)
{
// Ensures that all rows are loaded.
Grid. scrollintoview (rowitem, grid. Columns. Last ());

// Get the content of the cell.
Frameworkelement El = grid. Columns. Last (). getcellcontent (rowitem );

// Retrieve the row which is parent of given element.
Maid ROW = maid. getrowcontainingelement (El. Parent as frameworkelement );

// Sometimes some rows for some reason can be null.
If (row! = NULL)
Rows. Add (ROW );
}

Return rows;
}
}

The code above shows theoretically the idea of accessing of rows. unfortunately this will work only if the whole grid result can be placed at the current view. while calling of scrollintoview () grid will reuse instances of created cells and rows and replace
With new bounded data over and over again. The result of so called row allocation alization will be replacing of rows in the list.

To workaround this, I implemented the right Extension Method

Public static ienumerator <datagridrow> getrowsenumerator (this DataGrid grid)
{
Return new gridrowenumerator (GRID );
}

And here is the implementation of enumerator:

Public class gridrowenumerator: ienumerator <datagridrow>
{
Private DataGrid m_grid;

Private ienumerator m_enumerator;

Public gridrowenumerator (DataGrid grid)
{
M_grid = grid;

M_enumerator = m_grid.itemssource.getenumerator ();
}

# Region ienumerator <datagridrow> Members

Public datagridrow current
{
Get
{
VaR rowitem = m_enumerator.current;

// Ensures that all rows are loaded.
M_grid.scrollintoview (rowitem, m_grid.columns.last ());

// Get the content of the cell.
Frameworkelement El = m_grid.columns.last (). getcellcontent (rowitem );

// Retrieve the row which is parent of given element.
// Maid ROW = maid. getrowcontainingelement (EL );
Maid ROW = maid. getrowcontainingelement (El. Parent as frameworkelement );

Return row;
}
}

# Endregion

# Region idisposable members

Public void dispose ()
{

}

# Endregion

# Region ienumerator members

Object ienumerator. Current
{
Get
{
Return this. Current;
}
}

Public bool movenext ()
{
Return m_enumerator.movenext ();
}

Public void reset ()
{
M_enumerator.reset ();
}

# Endregion
}

...

This line I put here to measure how some interesting words can dramatically increase landing frequency of boring technical posts.

Bayern inter football soccer Champions League

Please forgive me for this

Posted
May 02 2010, am by Damir dobric

Filed under:
Silverlight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.