0

我希望你能帮忙。

当该列datagridview的数据应来自storedProcedure. 它只是随 that 返回的一个值sp。我不确定这是否可能,或者我应该在字符串中包含一个sql执行该任务的函数?sql

4

1 回答 1

0

如何以编程方式执行此操作.. 例如,您单击显示加载的按钮.. 这是加载存储过程的典型代码示例.. 您可以将代码移动到 page_load 而不是 Button1_click.. 我只是发布重要的代码片段..

public partial class WebForm4 : System.Web.UI.Page {
SqlConnection sqlcon = new SqlConnection("Data Source=Local-host;Initial 
                                      Catalog=HRM_Login;Integrated Security=True");
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter sqladp = new SqlDataAdapter();
        DataSet ds = new DataSet();

    protected void Button1_Click(object sender, EventArgs e)
            {
                Label2.Visible = true;
                GridView1.Visible = true;

                sqlcmd.Connection = sqlcon;
                sqlcmd.CommandText = "GetStudentName";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add(new SqlParameter("@Studentid", SqlDbType.Int)).Value 
                                          = Int32.Parse(txt_studentid.Text);
                sqladp.SelectCommand = sqlcmd;
                sqladp.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
    }

well u can try
GridView1.DataSource = ds; instead of GridView1.DataSource = ds.Tables[0];
于 2013-01-23T00:37:33.860 回答