2011-12-15 22 views
1

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

+2

这种简单的事情应该由谷歌搜索的解决方案也有数不胜数的例子在网上解决。 – 2011-12-15 13:01:19

回答

4

试试这个

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

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

使用GridView的DataBind()方法来做到这一点。像

GridView.DataSource = ds; 
GridView.DataBind(); 
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方法返回一个数据集与数据。