2013-02-27 65 views
0

我得到了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(); 
       } 

回答

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> 
+0

指定欄目,然後我看到5列(時間,內容,日期,時間,什麼) – 2013-02-27 13:19:11

+2

確保AutoGenerateColumns =「false」 – happygilmore 2013-02-27 13:27:44

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") }); 
相關問題