我在数据库中有两个表:
=========================
|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>
<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
public class Roll
{
public int Roll_number{get; set;}
public string Section {get; set;}
}
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;
}
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}}和Roll_no
未在网格视图中显示。
答案 0 :(得分:0)
DataGridVew
对象不显示嵌套类,因为它没有“装备”这样做。我的解决方案是创建一个只包含要显示的属性的类,填充它并将其用作数据源,或者遵循this复杂但有效的方法将单元格绑定到属性。
为了清楚起见,这里的问题不是linq,而是DataGridView
类中的限制。
答案 1 :(得分:0)
为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;
}