如何在ListView / GridView中显示DataSet

时间:2011-12-15 12:57:06

标签: c# asp.net dataset

假设我从数据库中检索了一个DataSet。之后我想在ASP.Net C#的ListView / GridView上显示。我怎样才能做到这一点?我的样品是什么?

3 个答案:

答案 0 :(得分:4)

试试这个

if(datasetObject.tables.count > 0)
    {
    GridView.DataSource = datasetObject;
    GridView.DataBind();

    }
else
{
  lable.Text = "No Record Found";
}

答案 1 :(得分:1)

使用GridView的DataBind()方法执行此操作。像

GridView.DataSource = ds;
GridView.DataBind();

答案 2 :(得分:1)

将数据集设置为网格的DataSource属性值,然后调用DataBind()方法。

来自msdn

http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx

void Page_Load(Object sender, EventArgs e)
{
// This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the 
// page is first loaded. Thereafter, the values are
// stored in view state.                      
if(!IsPostBack)
{

  // Declare the query string.
  String queryString = 
    "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";

  // Run the query and bind the resulting DataSet
  // to the GridView control.
  DataSet ds = GetData(queryString);
  if (ds.Tables.Count > 0)
  {
    AuthorsGridView.DataSource = ds;
    AuthorsGridView.DataBind();
  }
  else
  {
    Message.Text = "Unable to connect to the database.";
  }

}   
}

假设AuthorsGridView是你的GridView控件的ID,GetData方法返回一个包含数据的数据集。