我创建了一个简单的.aspx
页面,其中包含一个主网格视图(GridView1),第二个网格视图(GridView2)位于第一个网格视图的模板字段中。
这些网格视图有SqlDataSource
个SqlDataSource1
和SqlDataSource2
。)
是否可以通过SqlDataDource2
(或GridView1)中的值过滤SqlDataSource1
(或GridView2)中的行?
我的意思是对于GridView1中的每一行,只有来自GridView2的行,其中GridView2.PlaceID和GridView1.PlaceID字段相等。 现在我们从GridView1中看到GridView2中每行的所有行。
代码在这里:
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DB Places %>" SelectCommand="SELECT [PlaceID], [Place] FROM [Places]" ProviderName="System.Data.SqlClient">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DB Devices %>" SelectCommand="SELECT [PlaceID], [DeviceID], [Date] FROM [Devices]" ProviderName="System.Data.SqlClient">
</asp:SqlDataSource>
<asp:GridView id="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Код_места" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="PlaceID" HeaderText="PlaceID" InsertVisible="False" ReadOnly="True" SortExpression="PlaceID">
</asp:BoundField>
<asp:BoundField DataField="Place" HeaderText="Place" SortExpression="Place">
</asp:BoundField>
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Devices">
<ItemTemplate>
<asp:GridView id="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="PlaceID" HeaderText="PlaceID" SortExpression="PlaceID">
</asp:BoundField>
<asp:BoundField DataField="DeviceID" HeaderText="DeviceID" ReadOnly="True" SortExpression="DeviceID">
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date">
</asp:BoundField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
提前致谢!
答案 0 :(得分:0)
您需要将SqlDataSource2
置于asp:TemplateField
内,并在defaultValue
代码(.cs
)内设置GridView1_RowDataBound
。像这样的东西。另请阅读MSDN Walkthrough: Creating a Nested GridView Control
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DB Places %>" SelectCommand="SELECT [PlaceID], [Place] FROM [Places]" ProviderName="System.Data.SqlClient">
</asp:SqlDataSource>
<asp:GridView id="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="PlaceID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="PlaceID" HeaderText="PlaceID" InsertVisible="False" ReadOnly="True" SortExpression="PlaceID">
</asp:BoundField>
<asp:BoundField DataField="Place" HeaderText="Place" SortExpression="Place">
</asp:BoundField>
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Devices">
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DB Devices %>"
SelectCommand="SELECT [PlaceID], [DeviceID], [Date] FROM [Devices] where PlaceID=@placeid" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:Parameter name="placeid" Type="Int32" DefaultValue="" />
</asp:SqlDataSource>
<asp:GridView id="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="PlaceID" HeaderText="PlaceID" SortExpression="PlaceID">
</asp:BoundField>
<asp:BoundField DataField="DeviceID" HeaderText="DeviceID" ReadOnly="True" SortExpression="DeviceID">
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date">
</asp:BoundField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
//.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find data source and set value for parameter
((SqlDataSource)e.Row.FindControl("SqlDataSource2")).SelectParameters["placeid"].DefaultValue = ((System.Data.DataRowView)e.Row.DataItem)["placeid"].ToString();
}
}