【问题标题】:ASP only shows the first gridview when var type is usedASP 仅在使用 var 类型时显示第一个 gridview
【发布时间】:2018-09-17 13:59:24
【问题描述】:

我只是 ASP 和 C# 的初学者。在两个 GridView 下运行时,只需在 default.aspx 页面中获取 Gridview1。也尝试在 aspx 代码中定义 DataSourceId 但没有成功。看起来 ASP 只显示在代码中找到的第一个 gridview,而忽略了第二个。非常感谢您的帮助!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using CsvHelper.TypeConversion;
using CsvHelper;
using System.IO;
sing CsvHelper.Configuration;
using System.Data;
using System.Windows;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader(Server.MapPath(@"MAD-DC01.MAD-TEST.ES.201808.csv"));
        CsvReader csvread = new CsvReader(sr);

        IEnumerable<TestRecord> record = csvread.GetRecords<TestRecord>();
        var Prints = from t in record     
                     group t by new { t.Airline, t.Kiosk_name,t.Type_print } into grupo
                     orderby grupo.Key.Airline ascending
                     select new
                            {
                                 Aerolinea = grupo.Key.Airline,
                                 Kiosko = grupo.Key.Kiosk_name,
                                 Impresion=grupo.Key.Type_print,
                                 cuenta = grupo.Sum(x => x.Prints_outs),
                            };
    var Total_Prints = from t2 in record
                       group t2 by new { t2.Airline,t2.Type_print } into grupo2
                       orderby grupo2.Key.Airline ascending
                       select new
                              {
                                  Aerolinea = grupo2.Key.Airline,
                                  Impresion = grupo2.Key.Type_print,
                                  cuenta = grupo2.Sum(x => x.Prints_outs),
                              }; 

    GridView1.DataSource = Prints.ToList();
    GridView1.DataBind();

    GridView2.DataSource = Total_Prints.ToList();
    GridView2.DataBind();
}

public class TestRecord // Test record class
{
    public DateTime Date { get; set; }
    public string Airport { get; set; }
    public string Type_print { get; set; }
    public string Airline { get; set; }
    public string Kiosk_name { get; set; }
    public Int32 Prints_outs { get; set; }

}

这是aspx部分:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
 <div>

     <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
     </asp:GridView>
     <asp:GridView ID="GridView2" runat="server" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
     </asp:GridView>
     <br />

 </div>
</form>
</body>
</html>

【问题讨论】:

  • GridView2.DataSource = 行放置一个断点,然后运行它。当它到达断点时,告诉我们Total_Prints 中有多少项目。
  • Total_Prints 为空是个好消息。这就是我期望您的问题引起的。我不确定您评论的其余部分是什么意思,因为它似乎与您的问题相同。哇...您的评论已被删除。
  • 对不起,FF的一些问题,我的意思是如果以下几行被注释;//GridView1.DataSource = Prints.ToList(); //GridView1.DataBind();,GridView2 被正确填充,这听起来很奇怪。谢谢
  • 好吧,计算机不是魔法……一定是有什么东西把它放在了DataGrid中。目前,您是世界上唯一可以访问您的代码的人(这不是问题)。我认为您需要将其归结为minimal reproducible example,以便其他人可以重复。目前,您的问题包括与问题无关的内容,例如 CSV 处理和 Linq 查询。阅读有关 MCVE 的信息,然后尝试使您的问题更加集中。正如 MCVE 页面上所说,您甚至可以在途中自己找到答案...

标签: c# asp.net gridview


【解决方案1】:

我已经通过替换解决了这个问题

IEnumerable<TestRecord> record = csvread.GetRecords<TestRecord>();  

作者:

List<TestRecord> record = new List<TestRecord>();  
while (csvread.Read()) {  
    TestRecord Record = csvread.GetRecord<TestRecord>();
    record.Add(Record);}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多