日期:2014-05-17  浏览次数:20415 次

datatable添加新列问题
我想添加一个新列url代码如下:
aspx.cs:
string strcon = "Data Source=localhost; Initial Catalog=Netshop; Integrated Security=SSPI";
 public DataTable excuteselect(string sqlstr)
  {
  SqlConnection conn=new SqlConnection(strcon);
  conn.Open();
  SqlCommand cmd=new SqlCommand();
  cmd.Connection=conn;
  cmd.CommandText=sqlstr;
  SqlDataAdapter sda=new SqlDataAdapter();
  sda.SelectCommand=cmd;
  DataSet ds=new DataSet();
  sda.Fill(ds,"tb_goodsinfo");
  return ds.Tables["tb_goodsinfo"];
  }
protected void Page_Load(object sender, EventArgs e)
  {
  string sqlstr = "select [goodsid],[goodsname],[goodsprice],[goodsphoto],[goodstypename] from tb_goodsinfo join tb_goodstype on tb_goodsinfo.goodstypeid=tb_goodstype.goodstypeid where tb_goodsinfo.goodstypeid='" + Request.QueryString["typeid"].ToString() + "';";
  DataTable dt = new DataTable();
  dt = excuteselect(sqlstr);
  DataColumn columnurl = new DataColumn("url");
  dt.Columns.Add(columnurl);
  columnurl.Caption = "url";
  columnurl.ColumnName = "url";
  foreach (DataRow drrow in dt.Rows)
  {
  drrow["url"] = "goodsdetails.aspx?id=" + drrow["goodsid"].ToString();
  } 
  dlsortshow.DataSource = dt.DefaultView;
  dlsortshow.DataBind();
  }
 aspx:  
  <asp:DataList ID="dlsortshow" runat="server" Width="850px" RepeatDirection="Horizontal" RepeatColumns="3">
  <ItemTemplate>
  &nbsp;&nbsp;&nbsp;&nbsp;<asp:ImageButton ID="ibtphoto" runat="server" Width="218px" Height="230px" ImageUrl='<%#Eval("goodsphoto") %>'/>&nbsp;&nbsp;&nbsp;&nbsp;<br />
  <asp:LinkButton ID="lbtname" runat="server" Text='<%#Eval("goodsname") %>' PostBackUrl='<%#Eval("url") %>'></asp:LinkButton><br />
  售价:¥<asp:Label ID="lbprice" runat="server" Text='<%#Eval("goodsprice") %>'></asp:Label><br /><br />
  </ItemTemplate>
  </asp:DataList>  

运行结果显示:DataBinding:“System.Data.DataRowView”不包含名为“url”的属性。 
也就是意味着添加新列没有成功,请问该怎样添加这一新列url呢

------解决方案--------------------
dt.Columns.Add("url",typeof(string));
------解决方案--------------------
在excuteselect方法中填加。
------解决方案--------------------
要先创建schema然后再填充数据。