简体   繁体   English

如何在Web窗体中使用linq查询在网格视图中显示数据

[英]how to display the data in grid view using linq query in web form

I have two tables in a database: 我在数据库中有两个表:

=========================
|Roll_number |  Section |
=========================
|1           | A        |   
|2           | B        |
|3           | C        |
=========================

=====================================
|Roll_no | Name   | Age | Address   |
=====================================
|1       | nikita | 23  | Bangalore |
|2       | ankita | 29  | hyderabad |
|3       | nidhi  | 35  | pune      |
=====================================

on selecting the name from dropdown list, I want to display all the data in grid view. 从下拉列表中选择名称时,我想在网格视图中显示所有数据。 The entity classes which I have made are nested. 我制作的实体类是嵌套的。

My code: Display.aspx 我的代码:Display.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Grid1.aspx.cs" Inherits="Display.Grid1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>nikita</asp:ListItem>
            <asp:ListItem>ankita</asp:ListItem>
            <asp:ListItem>nidhi</asp:ListItem>
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="display data" />
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />

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

Roll.cs Roll.cs

 public class Roll
    {
         public int Roll_number{get; set;}
         public string Section {get; set;}

    }

info.cs info.cs

public class info
    {
         public Roll roll { get; set; }
         public string name { get; set; }
         public int age { get; set; }
         public string address { get; set; }
    }

dataAccess.cs dataAccess.cs

public List<info> Getdetails(string name)
         {
             List<info> lst = new List<info>();
             DataClasses1DataContext con = new DataClasses1DataContext();
             var res = (from c in con.Datavalues
                       join d in con.Details on c.Roll_no equals d.Roll_number
                       where c.Name == name
                       select new { c.Name, c.Age, c.Address, c.Roll_no, d.Section }).ToList();
             foreach (var r in res)
             {
                 info info= new info();
                 Roll roll1 =new Roll();
                 roll1.Roll_number=Convert.ToInt32(r.Roll_no);
                 roll1.Section = r.Section;
                 info.address = r.Address;
                 info.age = Convert.ToInt32(r.Age);
                 info.name = r.Name;
                 info.roll = roll1;
                 lst.Add(info);

             }
             return lst;

         }

display.aspx.cs display.aspx.cs

 protected void Button1_Click(object sender, EventArgs e)
            {

            DataAcess da = new DataAcess();
            List<info> lst = new List<info>();
            info inf = new info();

            inf.name = DropDownList1.Text;

            List<info> lsto = new List<info>();

           lst= da.Getdetails(inf.name);

           foreach (var res in lst)
           {

               info info = new info();
               Roll rol = new Roll();
               rol.Roll_number = res.roll.Roll_number;
               rol.Section = res.roll.Section;
               info.roll = rol;
               info.address = res.address;
               info.age = res.age;
               info.name = res.name;

               lsto.Add(info)      ;
           }
           GridView1.DataSource = lsto;

           GridView1.DataBind();
   }

While running this code, I can only find the details of name, age, and address. 在运行此代码时,我只能找到姓名,年龄和地址的详细信息。 Roll_no and section are not displaying in grid view. Roll_nosection未显示在网格视图中。

A DataGridVew object doesn't display nested classes because it's not "equipped" to do so. DataGridVew对象不显示嵌套类,因为它没有“装备”这样做。 My solution would be to create a class only with the attributes to be displayed, populate that, and use it as data source, or follow this complicated, but efficient way of binding cells to attributes. 我的解决方案是仅使用要显示的属性创建一个类,然后填充该类并将其用作数据源,或者遵循将单元格绑定到属性的这种复杂但有效的方法。

Just to be clear, your problem here is not linq, but the limitations within the DataGridView class. 只是要清楚,这里的问题不是linq,而是DataGridView类中的限制。

try this method for getDetails 尝试使用此方法获取getDetails

public List<info> Getdetails(string name)
         {
             List<info> lst = new List<info>();
             DataClasses1DataContext con = new DataClasses1DataContext();
             var res = (from c in con.Datavalues
                       join d in con.Details on c.Roll_no equals d.Roll_number
                       where c.Name == name
                       select new info() { c.Name, c.Age, c.Address, c.Roll_no, d.Section }).ToList();
             foreach (var r in res)
             {
                 info info= new info();
                 Roll roll1 =new Roll();
                 roll1.Roll_number=Convert.ToInt32(r.Roll_no);
                 roll1.Section = r.Section;
                 info.address = r.Address;
                 info.age = Convert.ToInt32(r.Age);
                 info.name = r.Name;
                 info.roll = roll1;
                 lst.Add(info);



             }
             return lst;


         }

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

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