0

データベース ビューのデータを Gridview に入力しようとしています。ビューには表示する必要がある200 + k行があるため、linqを使用できません。ここに私のコードがあります:

     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>

グリッドビューがまったくない空白のページが表示されます。私は何を間違っていますか?

4

4 に答える 4

2
enter code here

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

これを試してみると、すべての詳細がリーダーに取得され、グリッドに直接バインドされます。そこで何か他のことをしたい場合を除いて、すべての行をループする必要はありません。

于 2012-10-30T08:30:49.937 に答える
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();
}
于 2012-11-01T00:14:45.637 に答える
0

ref を宣言してみてください

public void GetReport(ref List<string> adr)

ref (C# リファレンス)

于 2012-10-30T14:20:23.847 に答える
0

空の GridView が表示されないため、クエリがデータを取得しているかどうかを確認します

于 2012-10-30T14:11:11.190 に答える