locked
How to get DataTable from System.Windows.Controls.DataGrid RRS feed

  • Question

  • I need to create a function giveing DataGrid to the function and the function returns DataTable containing all columns and rows in the datagrid. I am using System.Windows.Controls.DataGrid. Unfortunetly DataGrid has no properties like Rows so I can loop trough and get all the data. It does have propeties like Columns but I can not get Column datatype from it.

    Im stuck, please advice.

    Monday, September 5, 2011 8:26 AM

All replies

  • Hi,

    try to create a new datatable object, after processing assign the datasource property to the datatable

    dim d as new datatable

    Grid Name=DataHolder

    d=dataholder.datasource

    Also for iterating through the columns of the DataGrid,try datagridviewcolumns

    http://social.msdn.microsoft.com/Search/en-us?query=datagridview

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

    Hope it helps

    Regards,

    A.Murugan

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, September 5, 2011 8:49 AM
  • I am using C# languge and System.Windows.DataGrid, not DataGridView.

    Monday, September 5, 2011 8:58 AM
  • If you have bound your DataGrid to a DataTable, you can easily get the dataTable as,   

    DataTable table = myDataGrid.DataSource as DataTable;
    

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, September 5, 2011 9:00 AM
  • In wpf datagrid, rows are ItemSource.items... no Rows property!

    Try itemsource property.

     var itemsSource = grid.ItemsSource as IEnumerable;
           

           
    foreach (var item in itemsSource)
           
    {
               
    var row = grid.ItemContainerGenerator.ContainerFromItem(item)
                                     
    as System.Windows.Controls.DataGridRow;
           
    }



    • Edited by Zain_Ali Monday, September 5, 2011 9:08 AM
    Monday, September 5, 2011 9:01 AM
  • No. My dataGrid itemssource is not bound to DataTable. My DataGrid.Itemssource is List<some object>.

     

    Zain_Ali:

    Ok now what I do have the DataGridRow, I still cant get objects from the row. I can not add DataGridRows to DataTable? I can only add DataRows to DataTable


    • Edited by GaiusBaltar Monday, September 5, 2011 9:39 AM
    Monday, September 5, 2011 9:35 AM
  • O.K try this

     private object ValidateColumn(string columnName, DataRow row)
        {
            DataTable table = row.Table;
            DataColumn column = table.Columns[columnName];
            object cellValue = row[column.ColumnName];
    return cellValue;
    }


    • Edited by Zain_Ali Monday, September 5, 2011 9:50 AM
    Monday, September 5, 2011 9:49 AM
  • No, I have not bound my DataGrid to DataTable. My DataGrid.Itemssource = List<some datatype>.

    Zain_Ali, how can I get items array now what I have row? I cant not add DataGridRow to DataTable. I can only add DataRows to DataTable and only DataRows have List<object> (itemsArray).

    Help me out, allmost there...

    Monday, September 5, 2011 10:39 AM