如何使用ID获取数据? [英] How to get the data using ID?
问题描述
嗨......我有嵌套网格....我想在网格视图中点击加图时显示数据...
我得到所有细节..但问题是每当我点击父网格中的加号图片显示表格详细信息中的所有数据...
但我想只显示与该特定ID相关的数据当我点击那张图片的时候...
可以帮助我吗?
For Instance
Table_Master有id,主要成员,性别等....
Table_details有id,依赖会员,性别,年龄.....等
现在我想编写查询以显示在子网格中...
<当我点击'+'时,
..我想要依赖成员的详细信息....
这两个表中的ID很常见...这是主要的关键id ....
我尝试过:
Hi...I have nested grid....I want to show data when clicking plus image in grid view...
I am getting all details..But the problem is whenever I'm clicking the plus image in parent grid It's showing all the data's in the table detail...
But I want to show only data related to that particular id when I'm clicking that image...
can any one help me?
For Instance
Table_Master have id,Primary member,gender, ....etc
Table_details have id,dependent member,gender,age.....etc
Now I want to write query to show in child grid...
when I'm clicking '+'..I want to the details of the dependent member....
ID in both table is common...That is primary key id....
What I have tried:
select a.name,a.age,a.Gender, r.Relationname as Relation from table_master b
inner join table_detail a on a.Enrollautoid=b.Enrollautoid and a.id=b.id
left join table_relation r on a.Relationautoid=r.Relationautoid
where MARK=0 or MARK is null and a.id=b.id and a.Enrollautoid=b.Enrollautoid
推荐答案
希望这会对你有帮助。我有包含ASP.NET代码。
Hope this will help you.I Have included the ASP.NET code for you.
public class Master
{
public Master()
{
}
public int Id { get; set; }
public string PrimaryMember { get; set; }
public string Gender { get; set; }
}
public class Details
{
public Details()
{
}
public int Id { get; set; }
public string DependentMember { get; set; }
public string Gender { get; set; }
public int MasterId { get; set; }
}
public static class GridRepository
{
public static List<Master> GetMasters()
{
var masters = new List<Master>
{
new Master{Id=1,PrimaryMember="VijaiAnand",Gender="Male"},
new Master{Id=2,PrimaryMember="BalaSundar",Gender="Male"}
};
return masters;
}
public static List<Details> GetDetails()
{
var Details = new List<Details>
{
new Details{Id=1,DependentMember="cera",Gender="Female",MasterId=1},
new Details{Id=2,DependentMember="John",Gender="Male",MasterId=1},
new Details {Id=3,DependentMember="Srini",Gender="Male",MasterId=2}
};
return Details;
}
}
<%@ 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 id="form1" runat="server">
<div>
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound" DataKeyNames="Id">
<Columns>
<asp:TemplateField HeaderText="DetailsGrid">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="DependentMember" HeaderText="DependentMember" />
<asp:BoundField DataField="Gender" HeaderText="Gender" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PrimaryMember" HeaderText="PrimaryMember" />
<asp:BoundField DataField="Gender" HeaderText="Gender" />
</Columns>
</asp:gridview>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindMasterGrid();
}
}
protected void BindMasterGrid()
{
var masters = GridRepository.GetMasters();
Gridview1.DataSource = masters;
Gridview1.DataBind();
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int masterId = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
masterId = Convert.ToInt32(Gridview1.DataKeys[e.Row.RowIndex].Value);
GridView grdDetails = e.Row.FindControl("GridView2") as GridView;
if (grdDetails != null)
{
grdDetails.DataSource = GridRepository.GetDetails().Where(id => id.MasterId == masterId);
grdDetails.DataBind();
}
}
}
}
你必须在Gridview1中包含+图像控制的逻辑,并通过JavaScript切换。
You have to Include the logic for + image control inside that Gridview1 and toggle that via JavaScript.
这篇关于如何使用ID获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!