2014-01-27 70 views
0

我有一個datagrid,我想填充我的查詢。下面的代碼在我的winform應用程序中工作,我不認爲asp和c#綁定數據會有所不同。我看到有人使用數據集和SqlCommand,但它似乎並不相關,因爲它在我的winform應用程序中工作。我怎樣才能改變這個爲ASP的GridView的工作?感謝您的時間和幫助。將數據綁定到gridview不顯示網格asp

獲取數據的方法:它應該返回兩列數據。

StringBuilder sqlString = new StringBuilder(); 
sqlString.Append("SELECT DISTINCT o.SALES_NUMBER AS [Sales Number], "); 
sqlString.Append("d.DropTime AS [Drop Time] "); 
sqlString.Append("FROM [L\\sqlexpress].F_MSS.dbo.Order_Detail o "); 
sqlString.Append("FULL OUTER JOIN "); 
sqlString.Append("[COMMAND\\sqlexpress].Practiceville.dbo.DropTime d "); 
sqlString.Append("ON o.SALES_NUMBER = d.SalesONumber "); 
sqlString.Append("Where o.SALES_NUMBER IS NOT NULL "); 
sqlString.Append("Order by o.SALES_NUMBER "); 

DataTable dt = null; 

SqlConnection dbConn = new SqlConnection(Properties.Settings.Default["Connection"].ToString()); 

     try 
     {//set data source 
      dt = DBHelper.executeDataTable(dbConn, sqlString.ToString(), null); 
      if (dt != null) 
      { 
       dropGridView.DataSource = dt; 
      } 

      dbConn.Close(); 
      dbConn.Dispose(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      if (dbConn != null) 
      { 
       try { dbConn.Close(); dbConn.Dispose(); } 
       catch { } 
      } 
     } 

回答

2

您需要進行數據綁定:

dropGridView.DataSource = dt; 
dropGridView.DataBind(); 
相關問題