Get Cell Value of DataGrid using Loop in WPF

Last Reply 23 days ago By pandeyism

Posted 23 days ago

Hi all

i have datagrid.

how to loop the all cell value in datagrid in wpf using C#

Thanks and regards

siddu

You are viewing reply posted by: pandeyism 23 days ago.
Posted 23 days ago

Hi sanvi,

Refer below sample.

Xaml

<Grid Height="193" Width="320">
    <DataGrid Height="120" Width="300" HorizontalAlignment="Left" Margin="12,21,0,0"   AutoGenerateColumns="False" 
Name="dgCustomers" VerticalAlignment="Top" RowHeight="20" ColumnWidth="100" >
        <DataGrid.Columns>
            <DataGridTextColumn Header = "Id" Binding = "{Binding ID}" />
            <DataGridTextColumn Header = "Name" Binding = "{Binding Name}" />
            <DataGridTextColumn Header = "Country" Binding = "{Binding Country}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Display" Height="26" HorizontalAlignment="Left" Margin="95,147,0,0" Name="button1" VerticalAlignment="Top" Width="66" Click="button1_Click" />
</Grid>

Code

C#

public MainWindow()
{
    InitializeComponent();
    dgCustomers.ItemsSource = LoadCollectionData();
}

private List<Customer> LoadCollectionData()
{
    List<Customer> customers = new List<Customer>();
    customers.Add(new Customer { ID = 1, Name = "Mudassar Khan", Country = "India", });
    customers.Add(new Customer { ID = 2, Name = "John Hammod", Country = "USA", });
    customers.Add(new Customer { ID = 3, Name = "Suzzain Mathew", Country = "France", });
    return customers;
}

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    string message = "";
    for (int i = 0; i < dgCustomers.Items.Count - 1; i++)
    {
        message = "";
        for (int j = 0; j < dgCustomers.Columns.Count; j++)
        {
            DataGridCell cell = GetCell(i, j);
            TextBlock tb = cell.Content as TextBlock;
            message += tb.Text + " ";
        }
        MessageBox.Show(message);
    }
}

public DataGridCell GetCell(int row, int column)
{
    DataGridRow rowData = GetRow(row);
    if (rowData != null)
    {
        DataGridCellsPresenter cellPresenter = GetVisualChild<DataGridCellsPresenter>(rowData);
        DataGridCell cell = (DataGridCell)cellPresenter.ItemContainerGenerator.ContainerFromIndex(column);
        if (cell == null)
        {
            dgCustomers.ScrollIntoView(rowData, dgCustomers.Columns[column]);
            cell = (DataGridCell)cellPresenter.ItemContainerGenerator.ContainerFromIndex(column);
        }
        return cell;
    }
    return null;
}

public DataGridRow GetRow(int index)
{
    DataGridRow row = (DataGridRow)dgCustomers.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        dgCustomers.UpdateLayout();
        dgCustomers.ScrollIntoView(dgCustomers.Items[index]);
        row = (DataGridRow)dgCustomers.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}

Screenshot