将数据绑定到gridview不显示网格asp

时间:2014-01-27 19:00:22

标签: c# asp.net gridview

我有一个数据网格我试图填充我的查询。下面的代码在我的winform应用程序中工作,我不认为asp和c#绑定数据会有所不同。我见过人们使用数据集和SqlCommand,但它似乎没有相关性,因为它在我的winform应用程序中工作。如何将其更改为ASP gridview?感谢您的时间和帮助。

获取数据的方法:它应该返回两列数据。

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 { }
            }
        }

1 个答案:

答案 0 :(得分:2)

您需要DataBind:

dropGridView.DataSource = dt;
dropGridView.DataBind();