将一个方法绑定到gridview
本文关键字:方法 绑定 gridview 一个 | 更新日期: 2023-09-27 18:16:10
我有麻烦绑定一个方法到gridview,有人可以帮助吗?基本上,我在getAllPatients method
返回一个数据集,然后将其传递到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;
}
下面是我的codebehind:
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();
}
}
Just set:
gridView1.DataMember = "Patients";
您仍然可以返回完整的集合(稍后可能会有更多的表,对吗?),并让DataMember决定在网格中显示哪些。
我发现了这个问题,感谢我在上海大学的讲师。
问题是我正在创建dataAdapter:
//create Data adapter
da = new SqlDataAdapter(sql, conn);
但没有创建数据集,就像我应该在这里:
//create Data Set
ds = new DataSet("patients");
因此为空白
这个链接也很有用http://support.microsoft.com/kb/314145