ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Get Cell Value of DataGrid using Loop in WPF<p>Hi <a class="username" rel="sanvi"> sanvi</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Xaml</strong></span></p> <pre class="brush: html">&lt;Grid Height=&#34;193&#34; Width=&#34;320&#34;&gt; &lt;DataGrid Height=&#34;120&#34; Width=&#34;300&#34; HorizontalAlignment=&#34;Left&#34; Margin=&#34;12,21,0,0&#34; AutoGenerateColumns=&#34;False&#34; Name=&#34;dgCustomers&#34; VerticalAlignment=&#34;Top&#34; RowHeight=&#34;20&#34; ColumnWidth=&#34;100&#34; &gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header = &#34;Id&#34; Binding = &#34;{Binding ID}&#34; /&gt; &lt;DataGridTextColumn Header = &#34;Name&#34; Binding = &#34;{Binding Name}&#34; /&gt; &lt;DataGridTextColumn Header = &#34;Country&#34; Binding = &#34;{Binding Country}&#34;/&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;Button Content=&#34;Display&#34; Height=&#34;26&#34; HorizontalAlignment=&#34;Left&#34; Margin=&#34;95,147,0,0&#34; Name=&#34;button1&#34; VerticalAlignment=&#34;Top&#34; Width=&#34;66&#34; Click=&#34;button1_Click&#34; /&gt; &lt;/Grid&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public MainWindow() { InitializeComponent(); dgCustomers.ItemsSource = LoadCollectionData(); } private List&lt;Customer&gt; LoadCollectionData() { List&lt;Customer&gt; customers = new List&lt;Customer&gt;(); customers.Add(new Customer { ID = 1, Name = &#34;Mudassar Khan&#34;, Country = &#34;India&#34;, }); customers.Add(new Customer { ID = 2, Name = &#34;John Hammod&#34;, Country = &#34;USA&#34;, }); customers.Add(new Customer { ID = 3, Name = &#34;Suzzain Mathew&#34;, Country = &#34;France&#34;, }); 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 = &#34;&#34;; for (int i = 0; i &lt; dgCustomers.Items.Count - 1; i++) { message = &#34;&#34;; for (int j = 0; j &lt; dgCustomers.Columns.Count; j++) { DataGridCell cell = GetCell(i, j); TextBlock tb = cell.Content as TextBlock; message += tb.Text + &#34; &#34;; } MessageBox.Show(message); } } public DataGridCell GetCell(int row, int column) { DataGridRow rowData = GetRow(row); if (rowData != null) { DataGridCellsPresenter cellPresenter = GetVisualChild&lt;DataGridCellsPresenter&gt;(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&lt;T&gt;(Visual parent) where T : Visual { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i &lt; numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); child = v as T; if (child == null) { child = GetVisualChild&lt;T&gt;(v); } if (child != null) { break; } } return child; }</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/a7Vqu3Y.gif" alt="" width="335" height="218" /></p>https://www.aspforums.net:443/Threads/517789/Get-Cell-Value-of-DataGrid-using-Loop-in-WPF/https://www.aspforums.net:443/Threads/517789/Get-Cell-Value-of-DataGrid-using-Loop-in-WPF/Fri, 23 Aug 2019 05:56:14 GMT