2014-10-09 88 views
0

我有日曆,根據所選日期顯示信息。 它是這樣的:如何動態更新數據源C#?

string queryString = "SELECT * from events"; 
SqlCommand thisCommand = thisConnection.CreateCommand(); 
thisCommand.CommandText = queryString; 
SqlDataReader reader = thisCommand.ExecuteReader(); 
if(true) 
    queryString = "SELECT Text from events where repeat = 1"; 
else 
    queryString = "SELECT Text from events where repeat = 0"; 

GridView1.DataSource = reader ; 
GridView1.Bind(); 

我得到的錯誤是「數據源不支持服務器端數據分頁」

+0

代碼無法編譯...''queryString'在本示例中未正確聲明 – Dalorzo 2014-10-09 15:29:33

+0

我修復了它,我忘了粘貼初始聲明。 – User765876 2014-10-09 15:30:59

+0

你是否需要在你的用戶界面中使用「分頁」行爲? – 2014-10-09 15:43:27

回答

0

您應該使用SqlDataAdapter的,而不是來填充數據表,然後使用數據表作爲你gridview的數據源。 SqlReader是用於快速訪問sql數據的類,它不適合作爲數據源。

var adapter = new SqlDataAdapter(thisCommand); 
var table = new DataTable(); 
adapter.Fill(table); 

GridView1.DataSource = table; 
GridView1.Bind(); 

雖然您將在Web服務器上進行所有排序/分頁操作,但最好直接在SQL服務器上處理此操作。所以我建議使用ObjectDataSource或LinqDataSource並適當調整查詢來處理SQL服務器級別的分頁/排序。

+0

當我更改日曆的selectedDates時,我得到了這種懷疑:「已經有一個與此命令關聯的打開的DataReader,必須先關閉」 – User765876 2014-10-09 15:53:00