2011-12-15 62 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方法返回一個數據集與數據。