将数据表绑定到中继器

Bind Datatable to a Repeater

我正在尝试将数据表绑定到 aspx 上的转发器。我在 "DataBind" 方法中遇到下面提到的错误。从昨天开始,我已经厌倦了尝试解决这个问题。这是我的代码:

ASPX:

<asp:Repeater ID="HistoryList" runat="server">
  <HeaderTemplate>
    <table class="dataTable" style="width:100%;border-collapse: collapse;padding:0px 0px 0px 0px;">
      <tr>
        <td class="dataHeaderCell">#</td>
        <td class="dataHeaderCell">Date</td>
        <td class="dataHeaderCell">Dealer</td>
        <td class="dataHeaderCell">Security</td>
        <td class="dataHeaderCell">Bid</td>
     </tr>
  </HeaderTemplate>
    <ItemTemplate>
        <td class="dataCell right" title='<%# DataBinder.Eval(Container.DataItem, "HistoryId") %>'><%# Container.ItemIndex + 1 %></td>
        <td class="dataCell"><%# (null != DataBinder.Eval(Container.DataItem, "EffectiveDate")) ? ((DateTime)DataBinder.Eval(Container.DataItem, "EffectiveDate")).ToString("d") : "..."%></td>
        <td class="dataCell"><%# DataBinder.Eval(Container.DataItem, "OrganizationName") %></td>
        <td class="dataCell nowrap"><a target="_blank" title="Click Here to View Name..." href='NameView.aspx?page=27&nid=<%# DataBinder.Eval(Container.DataItem, "SecurityId") %>'><%# (60587 == (int)DataBinder.Eval(Container.DataItem, "NameId")) ? DataBinder.Eval(Container.DataItem, "Expr1").ToString() + " (Unknown)" : DataBinder.Eval(Container.DataItem, "Name").ToString() %></a></td>
        <td class="dataCell nowrap"><%# DataBinder.Eval(Container.DataItem, "Bid") %></td>
    </ItemTemplate>
  </table>
</asp:Repeater>  

CS.aspx:

  string dbConnect = System.Configuration.ConfigurationManager.AppSettings["dbConnect"].ToString();
  SqlConnection thisConnection = new SqlConnection(@dbConnect);
  thisConnection.Open();

  SqlCommand customCommand = new SqlCommand(queryString.ToString());
  customCommand.Connection = thisConnection;
  customCommand.CommandType = System.Data.CommandType.Text;

  DataTable dt = new DataTable();
  SqlDataAdapter adapter = new SqlDataAdapter(customCommand4);
  adapter.Fill(dt);

  DataTable newsDataTable = new DataTable();

  foreach(DataRow dr in dt.Rows)
  {
    //check for something 
    if (true)
    {
        newsDataTable.ImportRow(dr);
    }
  }

  if (0 < newsDataTable.Rows.Count)
  {
    HistoryList.DataSource = newsDataTable;
    HistoryList.DataBind();
    HistoryList.Visible = true;
    HistoryTitle.Text = string.Format(HistoryTitle.Text, HistoryColor.Count);
  }

我遇到的错误是:

    DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'HistoryId'. --->    at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
       at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts)
       at System.Web.UI.DataBinder.Eval(Object container, String expression)
       at ASP.comps_aspx.__DataBind__control60(Object sender, EventArgs e) in c:\Users\sk\Documents\Visual Studio 2013\Projects\DynamicWeb\Comps.aspx:line 562
       at System.Web.UI.Control.OnDataBinding(EventArgs e)
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.Control.DataBindChildren()
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem)
       at System.Web.UI.WebControls.Repeater.AddDataItemsIntoItemsArray(IEnumerable dataSource, Boolean useDataSource)
       at System.Web.UI.WebControls.Repeater.PostGetDataAction(IEnumerable dataSource)
       at System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource)
       at System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e)
       at System.Web.UI.WebControls.Repeater.DataBind()
       at ClarityDynamicWeb.Comps.Page_Load(Object sender, EventArgs e) in c:\Users\sk\Documents\Visual Studio 2013\Projects\DynamicWeb\Comps.aspx.cs:line 593

谁能指出我遗漏或做错了什么?谢谢。

已修改 DataTable newsDataTable = new DataTable(); DataTable newsDataTable = dt.Clone(); 而不是 newsDataTable.ImportRow(dr); 使用 newsDataTable.Rows.Add(dr.ItemArray);