3

How can you iterate in the rows and columns of a WPF DataGrid like with a Forms DataGridView in C#?

For example, if you have Forms DataGridView you can do something like this:

for(int i = 0; i < formsDataGrid1.Rows.Count; i++)
{
  MessageBox.Show(formsDataGrid1.Rows[i].ToString());
  for(int j = 0; j < formsDataGrid1.Columns.Count; j++)
     MessageBox.Show(formsDataGrid1.Rows[i].Cells[j].ToString());
}

Thank you for any help!

**Edit:

The reason I want to do this is that the DataGrid will be used by a user to enter certain informations in the second column of the DataGrid. Also, this DataGrid has multiple rows and I want to able to get that data and update a database with it.

Partial
  • 9,529
  • 12
  • 42
  • 57

3 Answers3

6

dg is your XAML DataGrid x:Name

    for (int i = 0; i < dg.Items.Count; i++)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
            for (int j = 0; j < dg.Columns.Count; j++)
            {
                TextBlock cellContent = dg.Columns[j].GetCellContent(row) as TextBlock;
                Console.WriteLine(cellContent.Text);
            }
    }
mariko
  • 71
  • 1
  • 3
4

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;

    ...
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 1
    How do you get the DataSource for a WPF toolkit Datagrid? – Partial Aug 18 '09 at 17:14
  • @ThomasLevesque , actually I need to access the grid, because I have an UserControl is a specific Column, and I need to attach an event for each element of that specific Column, you know a good way to do that? Thanks – MBen Sep 28 '11 at 11:35
  • @MBen, you can probably access a specific DataGridRow by using the ItemsContainerGenerator of the DataGrid, but from there I'm not sure how to retrieve the content of a specific column... – Thomas Levesque Sep 28 '11 at 12:25
  • @ThomasLevesque Thanks ,I will try. Otherwise how would you do it? should I avoid using a custom UserControl inside my grids? – MBen Sep 28 '11 at 18:00
  • 1
    @MBen, there's a solution [here](http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b7299e55-92e2-4a6b-8987-869fef8f22eb/). I don't think you should avoid it, it depends on what you need to do... but usually there is no need to access the control programmatically, you can usually handle most cases with bindings, triggers etc – Thomas Levesque Sep 28 '11 at 22:08
  • Thanks @ThomasLevesque, that's what I was looking for, avoid any "useless" code. I will let you know how it goes. – MBen Sep 29 '11 at 12:05
  • Well I implemented a clean solution using the DataContext of the user control and binding, much much cleaner way.. Thanks @ThomasLevesque – MBen Sep 30 '11 at 10:51
0

Try to use Grid.Column[x].GetCellContent(Row[y]) method

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
Alex
  • 8,827
  • 3
  • 42
  • 58
  • 2
    I take it you mean something like `dataGridObject.Columns[x].GetCellContent( ...something... );` But what can you enumerate over to get the `something` that goes in the parens? – ACK_stoverflow May 24 '12 at 19:26