Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将方法绑定到gridview_C#_Asp.net_Gridview - Fatal编程技术网

C# 将方法绑定到gridview

C# 将方法绑定到gridview,c#,asp.net,gridview,C#,Asp.net,Gridview,我无法将方法绑定到gridview,有人能帮忙吗?。 基本上,我在getAllPatients方法中返回一个数据集,然后将其传递到asp.net页面。当我去加载页面时,什么都没有发生 以下是我的方法: public DataSet GetAllPatients() { //create objects SqlConnection conn = null; SqlDataAdapter da = null; DataSet ds = null; string

我无法将方法绑定到gridview,有人能帮忙吗?。 基本上,我在
getAllPatients方法中返回一个数据集,然后将其传递到asp.net页面。当我去加载页面时,什么都没有发生

以下是我的方法:

public DataSet GetAllPatients()
{
    //create objects
    SqlConnection conn = null;
    SqlDataAdapter da = null;
    DataSet ds = null;
    string sql = string.Empty;

    //create sql string
    sql = "SELECT * FROM GP_Patient";

    //create connection
    conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GPConn"].ConnectionString);

    try
    {
        //create Data adapter
        da = new SqlDataAdapter(sql, conn);

        //fill dataset using data adapter
        da.Fill(ds, "Patients");
    }    
    catch
    {
        //don't do anything special, just let .Net handle it
    }    
    finally
    {
        // close connection and release data adapter
        if (conn != null)
        {
            conn.Close();
            da.Dispose();
        }
    }

    //output the dataset
    return ds;
}
下面是我的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //create object
        Patient ptn = new Patient();

        //set datasource of control to objects method
        gridview1.DataSource = ptn.GetAllPatients();


        //bind
        gridview1.DataBind();
    }
}
刚刚设定:

gridView1.DataMember = "Patients";

您仍然可以返回完整的集合(以后可能会有更多的表,对吗?),只需让DataMember指定要在网格中显示的表。

感谢我在SHU的讲师,我发现了这个问题

问题是我正在创建dataAdapter:

 //create Data adapter
            da = new SqlDataAdapter(sql, conn);
但我没有创建数据集,就像我在这里应该做的那样:

 //create Data Set
        ds = new DataSet("patients");
因此它是空白的


此链接也很有用

空的
捕获
不会“让.NET处理它”。异常的传播/冒泡会在捕获时停止,并抛出(重新)抛出,正常执行将继续。我想在这种情况下,可能会出现一个异常而不知道它。gridview在哪里?AutogeneratingColumns是否等于true?是的,AutogeneratingColumns现在设置为true,但没有区别DataMember将是“患者”查看此填充函数@DonBoitnott感谢您的帮助,但这似乎对我不起作用。为了彻底起见…应该在DataSource和DataBind()调用之前设置DataMember属性。