从存储过程获取结果以填充GridView

Dou*_*cil 6 asp.net data-binding gridview

我有一个Windows aspx表单,我有一个TextBox,Button和一个GridView.将TextBox被存储为变量@subschedule并传递到一个存储过程.我想做的是将该程序的结果填充到我的程序中GridView.有谁能建议这样做的方法?

谢谢

Nat*_*ate 0

您需要使用该DataSource属性:

DataTable dt = new DataTable();

// Open the connection 
using (SqlConnection cnn = new SqlConnection( 
       "Data Source=.\sqlexpress;Initial Catalog=AcmeRentals;Integrated Security=True")) 
{ 
    cnn.Open();

    // Define the command 
    using (SqlCommand cmd = new SqlCommand()) 
    { 
        cmd.Connection = cnn; 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.CommandText = storedProcedureName;

        // Define the data adapter and fill the dataset 
        using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
        { 
            da.Fill(dt); 
        } 
    } 
} 

// This is the key code; you can set the DataSource to "collection"
gridView.DataSource = dt.DefaultView;
gridView.DataBind();
Run Code Online (Sandbox Code Playgroud)

来源:http ://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-retrieve-a-datatable-using-a-stored-procedure.aspx