none
DataTableMapping RRS feed

  • Question

  • I have a method...

    public DataTable GetAllActivities()
            {
                DataTable act = new DataTable("Activities");
                dAdapt.Fill(act);                                             // SqlDataAdapter dAdapt
                return act;
            }

    which would be called typically as DataGridView.DataSource =
    GetAllActivities()

    I want to change the displayed column headers in the data grid but I'm not sure how to specify the column mappings and would appreciate an example.

    Thanks
    Wednesday, October 1, 2008 3:56 AM

Answers

  • The TableMappings.Add(<source TableName>, <target DataTable.TableName>)

    The <source TableName> must be unique in the TableMapping collection.

     

    When you use dAdapt.Fill(DataTable) overload it uses the DataTable.TableName to finding the appropriate mapping (ignoring the source TableName).

     

    So in your original example, your DataTable.TableName was "Activities" so it needs to be the second argument, not the first.  So try dAdapt.TableMappings.Add('<unique source TableName>", "Activities");

     

    When you use dAdapt.Fill(DataSet) overload, the adapter uses DbDataAdapter.DefaultSourceTableName which is "Table" for the first result set, then "Table1" for the second result set.  You can control this by using dAdapt.Fill(DataSet, "<source TableName>") overload where that strings is used for the first result set.  If you don't have a TableMapping, the srcTableName is then used for the DataTable.TableName.

     

     

     

    Friday, October 3, 2008 1:45 AM
    Moderator

All replies

  • if you know the column name in the database and the name you want in the datatable

    Code Snippet

    DataTableMapping tableMapping = dAdapter.TableMappings.Add("Activities", "Activities");

    tableMapping.ColumnMappings.Add("DatabaseColumn1", "DataTableColumn1");

    tableMapping.ColumnMappings.Add("DatabaseColumn2", "DataTableColumn2");

    tableMapping.ColumnMappings.Add("DatabaseColumn3", "DataTableColumn3");

    dAdapt.Fill(act);

     

     

     

    Wednesday, October 1, 2008 11:35 PM
    Moderator
  • Mark: I was doing

    DataTableMapping customMap = dAdapt.TableMappings.Add("Activities", "Training");

    modifying the column names as you show,  and placing the code just before the dAdapt.Fill(act) line. I was getting an error to the effect that "Activities" was defined more than once i.e. was not unique
    Thursday, October 2, 2008 3:29 AM
  • The TableMappings.Add(<source TableName>, <target DataTable.TableName>)

    The <source TableName> must be unique in the TableMapping collection.

     

    When you use dAdapt.Fill(DataTable) overload it uses the DataTable.TableName to finding the appropriate mapping (ignoring the source TableName).

     

    So in your original example, your DataTable.TableName was "Activities" so it needs to be the second argument, not the first.  So try dAdapt.TableMappings.Add('<unique source TableName>", "Activities");

     

    When you use dAdapt.Fill(DataSet) overload, the adapter uses DbDataAdapter.DefaultSourceTableName which is "Table" for the first result set, then "Table1" for the second result set.  You can control this by using dAdapt.Fill(DataSet, "<source TableName>") overload where that strings is used for the first result set.  If you don't have a TableMapping, the srcTableName is then used for the DataTable.TableName.

     

     

     

    Friday, October 3, 2008 1:45 AM
    Moderator
  • Mark: thanks - I've got it now.
    Friday, October 3, 2008 2:28 AM