Gridview 和 objectdatasource 的问题
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetObjects" TypeName="ControlPanelMessages">
</asp:ObjectDataSource>
我需要的是将两个项目放入一行。图像和名称应该位于同一行。
我该如何实现这一目标?
更新..我可以这样做吗:
DataRow newRow = table.NewRow();
// Set values in the columns:
newRow["Img"] = "NewCompanyImage";
newRow["Img"] = "NewCompanyName";
它将两个值放在同一行吗?
这就是我所做的:
public DataSet GetObjects()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
var source = from p in CommentsList
select new { p.Img, p.Name, p.Comment };
dt.Columns.Add("User");
dt.Columns.Add("Comment");
foreach (var item in source)
{
DataRow UserDetailsRow=dt.NewRow();
UserDetailsRow["User"] = item.Img;
UserDetailsRow["User"] = item.Name;
UserDetailsRow["Comment"] = item.Comment;
// dt.Rows.Add(new object[] { item.Img, item.Name, item.Comment });
}
ds.Tables.Add(dt);
return ds;
}
这是正确的方法吗?
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetObjects" TypeName="ControlPanelMessages">
</asp:ObjectDataSource>
What i need is to put two items into one row.. The Img and the Name should be in the same row.
How do I achieve that?
Update..can I do this:
DataRow newRow = table.NewRow();
// Set values in the columns:
newRow["Img"] = "NewCompanyImage";
newRow["Img"] = "NewCompanyName";
will it put both values in the same row?
Thats what i did:
public DataSet GetObjects()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
var source = from p in CommentsList
select new { p.Img, p.Name, p.Comment };
dt.Columns.Add("User");
dt.Columns.Add("Comment");
foreach (var item in source)
{
DataRow UserDetailsRow=dt.NewRow();
UserDetailsRow["User"] = item.Img;
UserDetailsRow["User"] = item.Name;
UserDetailsRow["Comment"] = item.Comment;
// dt.Rows.Add(new object[] { item.Img, item.Name, item.Comment });
}
ds.Tables.Add(dt);
return ds;
}
Is this the right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未向数据表添加任何列,
在添加行之前,您需要为其中想要的每一列执行此操作。
You haven't added any columns to the data table yet, you'll need to do
for each column you want in there before you add a row.