Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:06:04+00:00 2026-06-15T15:06:04+00:00

I created custom datatype classes that format the data the way I need it.

  • 0

I created custom datatype classes that format the data the way I need it. The data I retrieve from the database comes as .NET base types, so I need to loop through the DataTable, convert each item into it’s custom type, and put the converted item into a new table. I also copy the column names from the old table to the new one. The problem is, when I bind a GridView to my new table, it throws an exception:

HttpException:  
The data source for GridView with id 'TestGrid' did not have any properties
or attributes from which to generate columns.  Ensure that your data source 
has content.  

Stack Trace:
at System.Web.UI.WebControls.GridView.CreateAutoGeneratedColumns(PagedDataSource dataSource)
at System.Web.UI.WebControls.GridView.CreateColumns(PagedDataSource dataSource, Boolean useDataSource)
at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()

What do I need to add to my DataTable to get autogeneratecolumns to work?

Edit: Here is the code for the DataTable:

     // Populate first DataTable with data from database.
     adapter = new DataAdapter("SELECT status, date_ordered, date_due FROM import_table", OpenConnection);
     DataTable originalTable = new DataTable();
     adapter.Fill(originalTable)

     // Second DataTable for converted table data.
     DataTable convertedTable = new DataTable();

     // The list of custom datatypes to convert to.
     Type[] newTypes = {typeof(TrackingStatus), typeof(Date), typeof(Date)};

     // Set the ColumnName and DataType on each column of the new table.
     for(int i = 0; i < originalTable.Columns.Count; i++)
     {
        convertedTable.Columns.Add();
        convertedTable.Columns[i].ColumnName = originalTable.Columns[i].ColumnName;
        if(newTypes.Length > i)
           convertedTable.Columns[i].DataType = newTypes[i];
     }

     // Convert each item from the old table and add it to the new table.
     foreach(DataRow oldRow in originalTable.Rows)
     {
        DataRow newRow = convertedTable.NewRow();

        for(int i = 0; i < convertedTable.Columns.Count; i++)
        {
           if(newTypes.Length <= i)
              newRow[i] = oldRow[i];
           else if(newTypes[i] == typeof(Date))
              newRow[i] = Date.FromObject(oldRow[i]);
           else if(newTypes[i] == typeof(TrackingStatus))
              newRow[i] = TrackingStatus.FromObject(oldRow[i]);
           else if(newTypes[i] == typeof(EmailAddress))
              newRow[i] = EmailAddress.FromObject(oldRow[i]);
        }

        convertedTable.Rows.Add(newRow);
     }

     // Bind the GridView.
     displayGrid.DataSource = convertedTable;
     displayGrid.DataBind();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:06:06+00:00Added an answer on June 15, 2026 at 3:06 pm

    Instead of creating a new datatable when you convert each to custom types, create a new List and bind the list to the datasource. e.g.

    var list = new List<MyClass>();
    
    var myType = //Convert your type
    list.Add(myType);
    
    grid.DataSource = list;
    

    And ofcourse this all is just an idea of how to do it.

    UPDATE:
    Saw your code afterwards:

    Try this:

    Create a class called ImportRow

    class ImportRow
    {
        private string m_Status = string.Empty;
        private DateTime m_DateOrdered;
        private DateTime m_DateDue;
    
        public ImportRow() { }
    
        public string Status
        {
            get { return m_Status; }
            set { m_Status = value; }
        }
    
        public DateTime DateOrdered
        {
            get { return m_DateOrdered; }
            set { m_DateOrdered = value; }
        }
    
        public DateTime DateDue
        {
            get { return m_DateDue; }
            set { m_DateDue = value; }
        }
    
    }
    

    Then use it as:

            var importedData = new List<ImportRow>();
    
            foreach (DataRow oldRow in originalTable.Rows)
            {
                var newRow = new ImportRow();
    
                newRow.Status = oldRow["status"].ToString();
                newRow.DateDue = Convert.ToDateTime(oldRow["date_due"].ToString());
                newRow.DateOrdered = Convert.ToDateTime(oldRow["date_ordered"].ToString());
    
                importedData.Add(newRow);
            }
    

    Then

     displayGrid.DataSource = importedData;
     displayGrid.DataBind();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My jqGrid that does a great job of pulling data from my database, but
I am creating Linq expression trees from F# that operates on a custom datatype
I created a Custom Class that stores some data. One piece of data that
I have a set of custom data types that can be used to manipulate
I have created a custom data type enum like so: create type bnfunctionstype as
What I'm trying to do is create a new custom data type that behaves
I'm writing a console application that does a good amount of data retrieval from
i have created a LocalizedString custom data type for storing / displaying translations using
Is there a way to create a custom attribute that will make EF CodeFirst
I'm using google app engine/python. And I have created a custom datatype and property.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.