2011-05-25 36 views
4

DataSetDataTableLINQ表達式DataGridView的共同DataSource值。最佳方式的DataTable

現在相反,是否可以通過/綁定DataGridView記錄到DataSetDataTable

感謝

回答

1

得到了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; 
} 

示例:要使用這種方法,只需使用下面的代碼示例:

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

DataTable dt = LINQToDataTable(vrCountry); 

感謝

3

你可以做這樣的事情:

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; 
2

你可以試試這個:

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 

正是從這個網站摘錄: http://www.sourcehints.com/articles/how-to-convert-datagridview-data-to-datatable.html