C# 数据源需要具备什么才能使GridView自动生成列?

C# 数据源需要具备什么才能使GridView自动生成列?,c#,asp.net,data-binding,gridview,autogeneratecolumn,C#,Asp.net,Data Binding,Gridview,Autogeneratecolumn,我创建了自定义数据类型类,以我需要的方式格式化数据。我从数据库中检索到的数据是.NET基本类型,因此我需要在DataTable中循环,将每个项转换为其自定义类型,并将转换后的项放入新表中。我还将列名从旧表复制到新表。问题是,当我将GridView绑定到新表时,它会抛出一个异常: HttpException: The data source for GridView with id 'TestGrid' did not have any properties or attributes fro

我创建了自定义数据类型类,以我需要的方式格式化数据。我从数据库中检索到的数据是.NET基本类型,因此我需要在DataTable中循环,将每个项转换为其自定义类型,并将转换后的项放入新表中。我还将列名从旧表复制到新表。问题是,当我将GridView绑定到新表时,它会抛出一个异常:

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()
要使autogeneratecolumns正常工作,我需要向数据表中添加什么

编辑:以下是数据表的代码:

     // 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();
//使用数据库中的数据填充第一个数据表。
adapter=newdataadapter(“选择状态、订购日期、从导入表中到期日期”,OpenConnection);
DataTable Originatable=新DataTable();
适配器。填充(可原始)
//用于转换表数据的第二个DataTable。
DataTable convertedTable=新DataTable();
//要转换为的自定义数据类型的列表。
Type[]newTypes={typeof(TrackingStatus)、typeof(Date)、typeof(Date)};
//在新表的每一列上设置ColumnName和DataType。
for(int i=0;ii)
convertedTable.Columns[i].DataType=newTypes[i];
}
//转换旧表中的每个项目并将其添加到新表中。
foreach(Originatable.Rows中的DataRow oldRow)
{
DataRow newRow=convertedTable.newRow();
对于(int i=0;i如果(newTypes.Length您的自定义类型需要以下公共属性:

public class Foo
{
    public int Id;
    public string Name;
}

或者,当您将每个数据表转换为自定义类型时,您需要将
数据表本身用作
数据源

,而不是创建新的数据表,创建一个新列表并将列表绑定到数据源

var list = new List<MyClass>();

var myType = //Convert your type
list.Add(myType);

grid.DataSource = list;
然后将其用作:

        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);
        }

发布定义数据表的代码。事实上,您不能使用自定义类型列表,而不是将数据复制到数据表中吗?我将数据表用作数据源。自定义类型在其中。@Boric:为什么在数据表中使用自定义类型。如果可能,我会避免使用它。可以使用
数据表
列表
。我的网站有很多页面,可以检索和显示不同的数据库查询。我正在使用一个自定义日期对象等,它知道如何格式化自己,这样我就不会在每个页面中重复格式化代码。我不打算为每个查询创建自定义类型。列表将不起作用,因为我有多个列。此外,我不知道如何处理这解决了问题。请参阅我的评论。
        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);
        }
 displayGrid.DataSource = importedData;
 displayGrid.DataBind();