2011-12-01 78 views
0

我試圖將數據集作爲網格來源。所以,這是我所做的:將數據網格綁定到數據集

 SqlCommand cmd = new SqlCommand("TEST", 
               conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet d = new DataSet(); 
     da.Fill(d); 
     grid2.DataSource = d; 
     grid2.DataBind(); 

但是,我無法得到結果。網格在頁面上不可見。可以讓我知道如何做到這一點?

回答

1

您的代碼不完整;然而,這是如何做的:

//notice how the connection is enclosed in a using block 
using (SqlConnection conn = new SqlConnection("ConnectionString")) 
{ 
     conn.Open();//don't forget to open the connection 
     SqlCommand cmd = new SqlCommand("TEST",conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet d = new DataSet(); 
     da.Fill(d); 
     grid2.DataSource = d; 
     grid2.DataBind(); 
} 

順便說一句,你不需要DataAdapter。你可以簡單地做到這一點:

//notice how the connection is enclosed in a using block 
using (SqlConnection conn = new SqlConnection("ConnectionString")) 
{ 
     conn.Open();//don't forget to open the connection 
     SqlCommand cmd = new SqlCommand("TEST",conn); 
     cmd.CommandType = CommandType.StoredProcedure; 

     DataTable d = new DataTable(); 
     d.Load(cmd.ExecuteReader()); 
     grid2.DataSource = d; 
     grid2.DataBind(); 
} 
+0

我一直在做同樣的事情,但從未如此努力 – Sayamima

+0

@Nishanth:那麼你的proc沒有返回任何數據。 – Icarus

+0

是的我的過程運行良好。爲了測試它,我只寫了一條select *語句 – Sayamima