Accessing of Rows in Silverlight DataGrid
2011-08-20 08:14
253 查看
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 (DataGridRow rowItem 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 (DataGridRow rowItem in grid.GetRows())
{
. . .
}
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.
DataGridRow row = DataGridRow.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 virtualization 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.
//DataGridRow row = DataGridRow.GetRowContainingElement(el);
DataGridRow row = DataGridRow.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, 12:30 AM by Damir Dobric
Filed under:
Silverlight
For example, you want to do something like this:
foreach (DataGridRow rowItem 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 (DataGridRow rowItem in grid.GetRows())
{
. . .
}
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.
DataGridRow row = DataGridRow.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 virtualization 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.
//DataGridRow row = DataGridRow.GetRowContainingElement(el);
DataGridRow row = DataGridRow.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, 12:30 AM by Damir Dobric
Filed under:
Silverlight
相关文章推荐
- quick count the number of the rows in a big table
- How to Display DataSet in Silverlight DataGrid
- ios 解决bug(2)---invalid number of rows in section 0. The number of rows contained。。。
- Highlighting Rows in an ASP.Net DataGrid...
- get all rows count of all tables in a mysql database.
- Invalid update: invalid number of rows in section 0. The number of rows contained in an ....
- Total number of even entries in first n rows of Pascal's triangle
- How to extend ASP.NET datagrid for multi-selection of data rows.
- How to: Change color of rows in GridView on mouseover
- 一个奇葩错误:Invalid update: invalid number of rows in section 0
- numberOfSectionsInTableView:,tableView:numberOfRowsInSecion:,tableView:cellForRowAtIndexPath:
- ORA-30926: unable to get a stable set of rows in the source tables
- [UIView numberofRowsInSection]unrecognized selector sent to instance 0x1808bef0 错误
- Grouping in Silverlight 3 DataGrid
- ORA-30926: unable to get a stable set of rows in the source tables
- ORA-30926: unable to get a stable set of rows in the source tables
- Flipping Bits in Memory Without Accessing Them: An Experimental Study of DRAM Disturbance Errors
- 删除tableview的section遇到crash:The number of rows contained in an existing section after the update
- Building a Simple DataGrid in Silverlight 4.0
- ORA-30926: unable to get a stable set of rows in the source tables