WPF iterate through datagrid
I think first think you want to do is to get all rows of your DataGrid
:
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
if (null != row) yield return row;
}
}
and then iterate through your grid:
var rows = GetDataGridRows(nameofyordatagrid);
foreach (DataGridRow row in rows)
{
DataRowView rowView = (DataRowView)row.Item;
foreach (DataGridColumn column in nameofyordatagrid.Columns)
{
if (column.GetCellContent(row) is TextBlock)
{
TextBlock cellContent = column.GetCellContent(row) as TextBlock;
MessageBox.Show(cellContent.Text);
}
}
How do you iterate through selected cells in datagrid for WPF?
DataGrid.SelectedCells
returns an DataGridCellInfo
in WPF:
foreach (DataGridCellInfo cell in AppraiseeDataGrid.SelectedCells)
{
DataGridColumn column = cell.Column;
object dataRowItem = cell.Item;
}
WPF DataGrid: How do you iterate in a DataGrid to get rows and columns?
Typically, you don't do that : you access the underlying data source instead of the DataGrid itself. For instance, assuming the data source is an IEnumerable<Foo>
:
foreach(Foo f in foos)
{
MessageBox.Show(f.Name);
}
EDIT:
You don't need to access explicitly a specific cell of the grid : if the grid is bound to a list of objects, the object's property will be automatically updated when the user edits the corresponding cell in the grid.
Simple example with a list of contacts :
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
...
ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
dataGrid.ItemsSource = contacts;
...
Looping Through WPF DataGrid Using foreach
When you're working with WPF, avoid always accessing UI artifacts directly.
Create in you ModelView a property Color, and bind it to the background color of a single row template of your DataGrid view.
So in order to change the color you will loop throw the ModelView collecton and set/read Color property of every single object binded to every single row. By changing it, if binding is correctly settuped, you will affect row UI color appearance.
For concrete sample you can look on:
How do I bind the background of a data grid row to specific color?
How to loop over the rows of a WPF toolkit Datagrid
this will return a 'row' in your datagrid
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
if (null != row) yield return row;
}
}
in wpf datagrid, rows are ItemSource.items... no Rows property!
Hope this helps...
Related Topics
A Dictionary Object That Uses Ranges of Values for Keys
PDF Compression with Itextsharp
Configureawait Pushes the Continuation to a Pool Thread
How to Make Linq Execute a (Sql) Like Range Search
If Strings Are Immutable in .Net, Then Why Does Substring Take O(N) Time
Get User Location by Ip Address
Why Is Linq .Where(Predicate).First() Faster Than .First(Predicate)
How to Create Custom Propertygrid Editor Item Which Opens a Form
Curiously Recurring Template Pattern and Generics Constraints (C#)
Strange Behaviour of Console.Readkey() with Multithreading
Ftpwebrequest Ftp Download with Progressbar
How to Create a Message Box with "Yes", "No" Choices and a Dialogresult
Difference Between System.Datetime.Now and System.Datetime.Today
This Row Already Belongs to Another Table Error When Trying to Add Rows