使用 While() 结构时 Gridview 不会填充。 C# ASP.NET

发布于 2024-11-04 10:34:16 字数 719 浏览 3 评论 0原文

我在使用此网格视图时遇到问题。我正在用查询填充它。但是,如果我使用 while(reader.Read()) 结构,它不会填充甚至不会出现。没有 while 结构,它工作得很好。但是,我需要访问两个特定字段。代码如下。

 SqlDataReader myReader;
 try
 {
     using (myConnection)
     {
         myConnection.Open();
         ArrayList arrliGames = new ArrayList();
         myReader = myCommand.ExecuteReader();

         decimal decTicketCost = 0;
         int intTicketCount = 0;

         while (myReader.Read ())
         {
             decTicketCost = Convert .ToDecimal (myReader ["TicketCost"]);
             intTicketCount =Convert .ToInt32 (myReader ["NumTickets"]);
         }

         //Binds listbox
         grdEvents.DataSource = myReader ;
         grdEvents.DataBind();
     }
 }

I am having problems with this grid view. I am populating it with a query. However, it will not populate or even appear if I use a while(reader.Read()) structure. Without the while structure, it works fine. However, I need to access two specific fields. The code is below.

 SqlDataReader myReader;
 try
 {
     using (myConnection)
     {
         myConnection.Open();
         ArrayList arrliGames = new ArrayList();
         myReader = myCommand.ExecuteReader();

         decimal decTicketCost = 0;
         int intTicketCount = 0;

         while (myReader.Read ())
         {
             decTicketCost = Convert .ToDecimal (myReader ["TicketCost"]);
             intTicketCount =Convert .ToInt32 (myReader ["NumTickets"]);
         }

         //Binds listbox
         grdEvents.DataSource = myReader ;
         grdEvents.DataBind();
     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

久隐师 2024-11-11 10:34:16

SqlDataReader是只向前的。当您第一次迭代行时,其中“没有剩下任何内容”可以稍后显示。

我建议您使用阅读器在内存中填充强类型列表,然后将 GridView 绑定到该列表。示例:

var myList = new List<TicketInfo>();
while (myReader.Read())
{
    myList.Add(new TicketInfo
    {
        TicketCost = Convert.ToDecimal(myReader["TicketCost"]),
        NumTickets = Convert.ToInt32(myReader["NumTickets"])
    });
}
grdEvents.DataSource = myList;
grdEvents.DataBind();

上面的代码示例假设您有一个名为 TicketInfo 的类,定义为:

class TicketInfo
{
    public decimal TicketCost { get; set; }
    public int NumTickets { get; set; }
}

如果您之前没有使用过泛型(例如示例中的 List) ,我建议您阅读有关该主题的内容< /a>.

The SqlDataReader is forward-only. When you first iterate over the rows, there is "nothing left" in it to display afterwards.

I suggest that you use the reader to populate a strongly-typed list in memory, and then bind the GridView to the list instead. Example:

var myList = new List<TicketInfo>();
while (myReader.Read())
{
    myList.Add(new TicketInfo
    {
        TicketCost = Convert.ToDecimal(myReader["TicketCost"]),
        NumTickets = Convert.ToInt32(myReader["NumTickets"])
    });
}
grdEvents.DataSource = myList;
grdEvents.DataBind();

The code example above assumes that you have a class called TicketInfo defined as:

class TicketInfo
{
    public decimal TicketCost { get; set; }
    public int NumTickets { get; set; }
}

If you haven't used generics (such as List<TicketInfo> in the example) before, I suggest you do some reading on the subject.

无妨# 2024-11-11 10:34:16

创建一个具有两个属性的类
1. dec门票费用
2. intTicketCount

现在在 while 循环中创建实例并将值分配给对象属性

并将其添加到列表中。

最后绑定列表。

create a class with two properties
1. decTicketCost
2. intTicketCount

now in while loop create instance and assign the value to the object properties

and add it in a list.

Finally bind the list.

一杯敬自由 2024-11-11 10:34:16

我猜您已将数据源设置为 myList 而不是 myReader

grdEvents.DataSource = myList;

编辑: 您需要在列表对象中添加其他列。

while (myReader .Read ())
{
//myList-- Add other columns you need to display in the gridview
//As I don't know the your Data Reader column, I can't give you exact mylist object

 myList.Add(new TicketInfo{TicketCost = Convert.ToDecimal(myReader["TicketCost"]),NumTickets = Convert.ToInt32(myReader["NumTickets"])  });
}

I guest you have set datasource to myList instead myReader

grdEvents.DataSource = myList;

Edit: You need to add other column in your list object.

while (myReader .Read ())
{
//myList-- Add other columns you need to display in the gridview
//As I don't know the your Data Reader column, I can't give you exact mylist object

 myList.Add(new TicketInfo{TicketCost = Convert.ToDecimal(myReader["TicketCost"]),NumTickets = Convert.ToInt32(myReader["NumTickets"])  });
}
暖伴 2024-11-11 10:34:16

你可以替换

while (myReader .Read ())                        
{                             
decTicketCost  = Convert .ToDecimal (myReader ["TicketCost"]);                            intTicketCount = Convert .ToInt32 (myReader ["NumTickets"]);                             
}                              
//Binds listbox                         
grdEvents.DataSource = myReader ;                         
grdEvents.DataBind();  


grdEvents.DataSource = myReader ;
grdEvents.DataBind();

然后 gridview 的一些值

希望这有帮助

You could replace

while (myReader .Read ())                        
{                             
decTicketCost  = Convert .ToDecimal (myReader ["TicketCost"]);                            intTicketCount = Convert .ToInt32 (myReader ["NumTickets"]);                             
}                              
//Binds listbox                         
grdEvents.DataSource = myReader ;                         
grdEvents.DataBind();  

to
grdEvents.DataSource = myReader ;
grdEvents.DataBind();

And then the gridview for some values

hope this help

寒江雪… 2024-11-11 10:34:16

将以下行替换

grdEvents.DataSource = myReader;

grdEvents.DataSource = myList;

replace the following line

grdEvents.DataSource = myReader;

with

grdEvents.DataSource = myList;
野心澎湃 2024-11-11 10:34:16

这个怎么样:

using (myConnection)
{
   myConnection.Open();
   DataSet ds = myCommand.ExecuteDataSet();

   //Binds listbox
   grdEvents.DataSource = ds;
   grdEvents.DataBind();                    
}
ForEach (DataRow dr in ds.tables[0].rows)
    myList.Add(new TicketInfo{TicketCost = Convert.ToDecimal(myReader["TicketCost"]),NumTickets = Convert.ToInt32(myReader["NumTickets"])  });  

How about this:

using (myConnection)
{
   myConnection.Open();
   DataSet ds = myCommand.ExecuteDataSet();

   //Binds listbox
   grdEvents.DataSource = ds;
   grdEvents.DataBind();                    
}
ForEach (DataRow dr in ds.tables[0].rows)
    myList.Add(new TicketInfo{TicketCost = Convert.ToDecimal(myReader["TicketCost"]),NumTickets = Convert.ToInt32(myReader["NumTickets"])  });  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文