简体   繁体   English

使用 While() 结构时未填充 Gridview。 C# ASP.Net

[英]Gridview not populating while using a While() structure. C# ASP.Net

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.但是,如果我使用 while(reader.Read()) 结构,它不会填充甚至出现。 Without the while structure, it works fine.如果没有 while 结构,它可以正常工作。 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();
     }
 }

The SqlDataReader is forward-only. SqlDataReader是只进的。 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.我建议您使用阅读器在 memory 中填充强类型列表,然后将 GridView 绑定到列表中。 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:上面的代码示例假设您有一个名为 TicketInfo 的TicketInfo定义为:

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 .如果您之前没有使用过 generics(例如示例中的List<TicketInfo> ),我建议您对该主题进行一些阅读

create a class with two properties 1. decTicketCost 2. intTicketCount创建具有两个属性的 class 1. decTicketCost 2. intTicketCount

now in while loop create instance and assign the value to the object properties现在在 while 循环中创建实例并将值分配给 object 属性

and add it in a list.并将其添加到列表中。

Finally bind the list.最后绑定列表。

I guest you have set datasource to myList instead myReader我来宾您已将数据源设置为myList而不是myReader

grdEvents.DataSource = myList;

Edit: You need to add other column in your list object.编辑:您需要在列表 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"])  });
}

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.DataSource = myReader;
grdEvents.DataBind(); grdEvents.DataBind();

And then the gridview for some values然后是 gridview 的一些值

hope this help希望这有帮助

replace the following line替换以下行

grdEvents.DataSource = myReader;

with

grdEvents.DataSource = myList;

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"])  });  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM