GridView在页面上不可见

时间:2012-10-30 07:50:18

标签: c# asp.net .net

我正在尝试使用数据库视图中的数据填充Gridview。我不能使用linq因为视图有200 + k行我必须显示。这是我的代码:

     public partial class _Default : System.Web.UI.Page
{
    private string mobileGateway = "MobileGateway";
    private List<string> addressReport = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        GetReport(addressReport);
    }

    public void GetReport(List<string> adr)
    {
        string connecntionString = ConfigurationManager.ConnectionStrings[mobileGateway].ConnectionString;

        using (SqlConnection connection = new SqlConnection(connecntionString))
        {
            try
            {
                connection.Open();
                string sqlCmd = "SELECT * from dbo.BarcodeWithLocation";
                using (SqlCommand command = new SqlCommand(sqlCmd, connection))
                {
                    command.CommandType = System.Data.CommandType.Text;
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            adr.Add("" + reader[0]);
                            adr.Add("" + reader[1]);
                            adr.Add("" + reader[2]);
                            adr.Add("" + reader[3]);
                            adr.Add("" + reader[4]);
                            adr.Add("" + reader[5]);
                            adr.Add("" + reader[6]);
                            adr.Add("" + reader[7]);
                            adr.Add("" + reader[8]);
                            adr.Add("" + reader[9]);
                            adr.Add("" + reader[10]);
                        }

                        Grid.DataSource = adr;
                        Grid.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR!!!!: " + ex.Message);
            }
        }
    }
}

}

aspx代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="AddressReporting._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="Grid" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False">
</asp:GridView>
</asp:Content>

我得到一个没有gridview的空白页面。我做错了什么?

4 个答案:

答案 0 :(得分:2)

enter code here

command.CommandType = System.Data.CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
Grid.DataSource = reader;
Grid.DataBind();

尝试这一点,它会将所有细节都输入到阅读器中并直接将其绑定到网格中。除非你想在那里做其他事情,否则无需遍历所有行。

答案 1 :(得分:1)

尝试这样的事情:

string sqlCmd = "SELECT * from dbo.BarcodeWithLocation";
using (SqlCommand command = new SqlCommand(sqlCmd, connection))
{
     DataTable dataTable = new DataTable();
     command.CommandType = System.Data.CommandType.Text;
     connection.Open();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(command);

     dataAdapter.Fill(dataTable);

     Grid.DataSource = dataTable;
     Grid.DataBind();
}

答案 2 :(得分:0)

检查您是否正在查询是否正在获取数据,因为您无法看到空的GridView

答案 3 :(得分:0)

尝试声明参考

public void GetReport(ref List<string> adr)

ref (C# Reference)