如何将列数据绑定到数据源?

时间:2013-02-27 12:59:18

标签: asp.net data-binding datasource

我得到的表有1行3列(日期,时间,内容),我想要数据源中的3列中的2列(时间,什么),我该怎么做?

var table = (from r in socialEvents.AsEnumerable()
                         where r.Field<DateTime>("Date") >= Calendar1.SelectedDate.Date &&
                               r.Field<DateTime>("Date") <= Calendar1.SelectedDate.AddDays(1)
                         select r).CopyToDataTable();    

    if (table.Rows.Count > 0)
                {
                    DataGrid1.Visible = true;
                    DataGrid1.DataSource = table;
                    DataGrid1.DataBind();
                }

2 个答案:

答案 0 :(得分:1)

我假设datagrid是GridView? 那么你应该做这样的事情

<asp:GridView runat="server" AutoGenerateColumns="false"  >
<Columns>
    <asp:BoundField DataField="what" HeaderText="what" />
    <asp:BoundField DataField="time" HeaderText="time"  />        
</Columns>

</asp:GridView>

答案 1 :(得分:1)

您可以在选择

中指定列
var table = (from r in socialEvents.AsEnumerable()
                         where r.Field<DateTime>("Date") >= Calendar1.SelectedDate.Date &&
                               r.Field<DateTime>("Date") <= Calendar1.SelectedDate.AddDays(1)
                         select new {time = r.Field<DateTime>("Date"), what = r.Field<data_type>("what") });