Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在web表单中使用linq查询在网格视图中显示数据_C#_Asp.net_Linq_Webforms - Fatal编程技术网

C# 如何在web表单中使用linq查询在网格视图中显示数据

C# 如何在web表单中使用linq查询在网格视图中显示数据,c#,asp.net,linq,webforms,C#,Asp.net,Linq,Webforms,我在数据库中有两个表: ========================= |Roll_number | Section | ========================= |1 | A | |2 | B | |3 | C | ========================= ===================================== |Roll_no | Nam

我在数据库中有两个表:

=========================
|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      |
=====================================
从下拉列表中选择名称时,我希望在网格视图中显示所有数据。我创建的实体类是嵌套的

我的代码: 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>
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

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;

         }
public List Getdetails(字符串名称)
{
List lst=新列表();
DataClasses1DataContext con=新DataClasses1DataContext();
var res=(从con.Datavalues中的c开始)
在c卷上加入d。详细信息c卷号等于d卷号
其中c.Name==Name
选择新的{c.姓名、c.年龄、c.地址、c.卷号、d.节});
foreach(在res中的var r)
{
信息=新信息();
滚动1=新滚动();
滚动1.滚动编号=转换为32(r.滚动编号);
ROLL 1.截面=r.截面;
info.address=r.address;
info.age=转换为32(r.age);
info.name=r.name;
info.roll=roll1;
第一次添加(信息);
}
返回lst;
}
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();
   }
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
DataAcess da=新的DataAcess();
List lst=新列表();
info inf=新信息();
inf.name=DropDownList1.Text;
List lsto=新列表();
lst=da.Getdetails(inf.name);
foreach(lst中的var res)
{
信息=新信息();
Roll rol=新辊();
rol.Roll\u编号=res.Roll.Roll\u编号;
rol.截面=res.roll.截面;
info.roll=rol;
info.address=res.address;
info.age=res.age;
info.name=res.name;
添加(信息);
}
GridView1.DataSource=lsto;
GridView1.DataBind();
}

运行此代码时,我只能找到姓名、年龄和地址的详细信息
Roll_no
section
不显示在网格视图中。

A
DataGridVew
对象不显示嵌套类,因为它没有“装备”这样做。我的解决方案是创建一个只包含要显示的属性的类,填充该类并将其用作数据源,或者遵循复杂但高效的方法将单元格绑定到属性


需要明确的是,这里的问题不是linq,而是
DataGridView
类中的限制。

尝试此方法获取详细信息

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;


         }
public List Getdetails(字符串名称)
{
List lst=新列表();
DataClasses1DataContext con=新DataClasses1DataContext();
var res=(从con.Datavalues中的c开始)
在c卷上加入d。详细信息c卷号等于d卷号
其中c.Name==Name
选择新信息(){c.姓名、c.年龄、c.地址、c.卷号、d.章节});
foreach(在res中的var r)
{
信息=新信息();
滚动1=新滚动();
滚动1.滚动编号=转换为32(r.滚动编号);
ROLL 1.截面=r.截面;
info.address=r.address;
info.age=转换为32(r.age);
info.name=r.name;
info.roll=roll1;
第一次添加(信息);
}
返回lst;
}

Ok,那么现在发生了什么。请详细说明它不起作用的原因。从你的问题中甚至不清楚它是否起作用。我想在从下拉列表中选择名称时显示所有列的详细信息,即roll_no、name、age、address部分。运行此代码时,我只能找到name、age、address的详细信息。Roll_no和section未显示在网格视图中Use res.Roll_number和res.section实体类嵌套时,它不会直接接受res.Roll_number和res.section。仍然存在相同的问题
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;


         }