4

DataSet, DataTable and linq expression are common DataSource values of a DataGridView.

Now in reverse, is it possible to pass/bind a DataGridView record to DataSet or DataTable?

Thanks

BizApps
  • 6,048
  • 9
  • 40
  • 62

3 Answers3

3

You could do something like this:

var dataTable = new DataTable();

Array.ForEach(
    dataGridView1.Columns.Cast<DataGridViewColumn>().ToArray(), 
    arg => dataTable.Columns.Add(arg.HeaderText, arg.ValueType));
Array.ForEach(
    dataGridView1.Rows.Cast<DataGridViewRow>().ToArray(), 
    arg => dataTable.Rows.Add(arg.Cells.Cast<DataGridViewCell>().Select(cell => cell.Value)));

return dataTable;
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2

You can try this:

Public Shared Function DataGridViewToDataTable(ByVal dtg As DataGridView,
    Optional ByVal DataTableName As String = "myDataTable") As DataTable
    Try
        Dim dt As New DataTable(DataTableName)
        Dim row As DataRow
        Dim TotalDatagridviewColumns As Integer = dtg.ColumnCount - 1
        'Add Datacolumn
        For Each c As DataGridViewColumn In dtg.Columns
            Dim idColumn As DataColumn = New DataColumn()
            idColumn.ColumnName = c.Name
            dt.Columns.Add(idColumn)
        Next
        'Now Iterate thru Datagrid and create the data row
        For Each dr As DataGridViewRow In dtg.Rows
            'Iterate thru datagrid
            row = dt.NewRow 'Create new row
            'Iterate thru Column 1 up to the total number of datagrid columns
            For cn As Integer = 0 To TotalDatagridviewColumns
                row.Item(cn) = dr.Cells(cn).Value
            Next
            'Now add the row to Datarow Collection
            dt.Rows.Add(row)
        Next
        'Now return the data table
        Return dt
    Catch ex As Exception
        Return Nothing
    End Try
End Function

It was excerpt from this site: http://www.sourcehints.com/articles/how-to-convert-datagridview-data-to-datatable.html

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
1

Got this Best Answer from Csharp Corner:

public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
    DataTable dtReturn = new DataTable();

    // column names 
    PropertyInfo[] oProps = null;

    if (varlist == null) 
        return dtReturn;

    foreach (T rec in varlist)
    {
        // Use reflection to get property names, to create table, Only first 
        // time, others will follow 
        if (oProps == null)
        {
            oProps = ((Type)rec.GetType()).GetProperties();
            foreach (PropertyInfo pi in oProps)
            {
                Type colType = pi.PropertyType;

                if ((colType.IsGenericType) && 
                    (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {
                    colType = colType.GetGenericArguments()[0];
                }
                dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
            }
        }
        DataRow dr = dtReturn.NewRow();

        foreach (PropertyInfo pi in oProps)
        {
            dr[pi.Name] = pi.GetValue(rec, null)==null ? DBNull.Value 
                                                       : pi.GetValue(rec,null);
        }

        dtReturn.Rows.Add(dr);
    }
    return dtReturn;
}

Example: To use this method, just use the following code sample:

var vrCountry = from country in objEmpDataContext.CountryMaster
                select new {country.CountryID,country.CountryName};

DataTable dt = LINQToDataTable(vrCountry);

Thanks

forsvarir
  • 10,749
  • 6
  • 46
  • 77
BizApps
  • 6,048
  • 9
  • 40
  • 62