I want to copy all the datagrid records into datatable without using any loop. For Ex:
Dim dt as New DataTable
dt = Datagrid1.Items
But this is not Working and giving an error message.
My Development platform is Visual studio 2010 and language is WPF with vb.net 4.0
This is the way to transfer all the records from DATAGRID to DATATABLE without using the LOOP.
VB:
Dim dt As New DataTable
dt = CType(DataGrid1.ItemsSource, DataView).ToTable
C#:
DataTable dt = new DataTable();
dt = ((DataView)DataGrid1.ItemsSource).ToTable();
To convert your dataGrid into data table whith header row you can follow this steps:
1) create the method to retrive the cell
static public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
2) Iterate through all items and pass the content to dada table "cell"
private void DataGridToDataTable()
{
DataTable dt = new DataTable();
var j = byte.MinValue;//header row handler
dt.Rows.Add();
foreach (DataGridColumn column in dataGrid1.Columns)
{
dt.Columns.Add(column.GetValue(NameProperty).ToString());
dt.Rows[byte.MinValue][j++] = column.Header;
}
//data rows handler
for (int i = byte.MinValue ; i < dataGrid1.Items.Count; i++)
{
dt.Rows.Add();
for (j = Byte.MinValue; j < dataGrid1.Columns.Count; j++)
{
DataGridCell dgc = GetCell(dataGrid1, i, j);
dt.Rows[i + 1][j] = ((dgc.Content) as TextBlock).Text;
}
}
}
Keep in mind to use this methods you must will reference this using
using System.Windows.Media;
using System.Data;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
And here a helper class just to use it.
/// <summary>
/// Class to help to retrive the Data Grid Contain
/// </summary>
public static class DataGridHelper
{
/// <summary>
/// Retrive the cell contains
/// </summary>
/// <param name="dg">DataGrid</param>
/// <param name="row">row</param>
/// <param name="column">column</param>
/// <returns>DataGrid Cell content</returns>
static public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
/// <summary>
/// Get row content
/// </summary>
/// <param name="dg">Datagrid</param>
/// <param name="index">Index</param>
/// <returns>DataGridRow</returns>
static public DataGridRow GetRow(DataGrid dg, int index)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
dg.ScrollIntoView(dg.Items[index]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With