[英]Open GridView2 by selecting GridView1 field
我试图尽我选择内GridView1的特定字段时,它在GridView2 Access数据库打开相关数据。
这是我页面上的代码。
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="MainView.aspx.vb" Inherits="DosimetryASPNET_WebApplication.MainView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
<asp:Button ID="btnMenuView" runat="server" Text="Return to Menu" Width="200px" OnClick="btnMenuView_Click" />
<br />
<br />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder4" runat="server">
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID">
<Columns>
<asp:BoundField DataField="BatchID" HeaderText="BatchID" InsertVisible="False" ReadOnly="True" SortExpression="BatchID" />
<asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" />
<asp:BoundField DataField="BatchSize" HeaderText="BatchSize" SortExpression="BatchSize" />
<asp:BoundField DataField="Priority" HeaderText="Priority" SortExpression="Priority" />
<asp:BoundField DataField="StartReq" HeaderText="StartReq" SortExpression="StartReq" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString1.ProviderName %>" SelectCommand="SELECT * FROM [AllBatches]"></asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID" Visible="False">
</asp:GridView>
</asp:Content>
这是我用来填充GridView1的代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New Northwind.AllBatchesDataTable
Using da As New NorthwindTableAdapters.AllBatchesTableAdapter
da.Fill(dt)
End Using
GridView1.DataSource = dt.DefaultView
GridView1.DataBind()
End Sub
我正在使用Access数据库,并且为了填充GridView2,我使用了第二个表。
对于GridView1我用下面列“AllBatches”表:BatchID - 产品 - BATCHSIZE - 优先级 - StartReq - 状态
对于GridView2,我希望调用表“ IngredientsTable ”。 它包含以下列: LotManufactID- 成分 - 实际 - 目标 - 最小 - 最大 -WeighinhDate- 状态 -IDBatchID
在Access数据库中,该表的工作方式就像我希望它在ASP.NET中一样。 当我单击“ AllBatches ”的“ BatchID ”时,它将在我单击的带有“ IngredientsTable ”数据的行下方的行中打开一个辅助表。 我在两个表之间的银行中创建了一个关系。
这样做可能太复杂了,但是我需要开发此功能,该功能至少需要在GridView2中打开与“ AllBatches ”的“ BatchID ”相关的“ IngredientsTable ”数据。
希望您成功地表达了我的怀疑和明确的态度。 我正在等待帮助。
许多可能的解决方案之一:-在GridView1中声明“选择”字段-定义GridView2列-当单击选择字段时,会引发onSelectedIndexChanged,并且在这种情况下,您将读取所单击行的BatchID,对成分表执行查询并在GridView2上显示结果
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="BatchID" HeaderText="BatchID" InsertVisible="False" ReadOnly="True" SortExpression="BatchID" />
<asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" />
<asp:BoundField DataField="BatchSize" HeaderText="BatchSize" SortExpression="BatchSize" />
<asp:BoundField DataField="Priority" HeaderText="Priority" SortExpression="Priority" />
<asp:BoundField DataField="StartReq" HeaderText="StartReq" SortExpression="StartReq" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID" Visible="False">
<Columns>
<asp:BoundField DataField="LotManufactID " HeaderText="LotManufactID" SortExpression="LotManufactID" />
<asp:BoundField DataField="Ingredient " HeaderText="Ingredient" SortExpression="Ingredient" />
<asp:BoundField DataField="Actual " HeaderText="Actual" SortExpression="Actual" />
<asp:BoundField DataField="Target " HeaderText="Target" SortExpression="Target" />
<asp:BoundField DataField="Minimum " HeaderText="Minimum" SortExpression="Minimum" />
<asp:BoundField DataField="Maximum " HeaderText="Maximum" SortExpression="Maximum" />
<asp:BoundField DataField="WeighinhDate " HeaderText="WeighinhDate" SortExpression="WeighinhDate" />
<asp:BoundField DataField="Status " HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="IDBatchID " HeaderText="IDBatchID" SortExpression="IDBatchID" />
</Columns>
</asp:GridView>
和代码
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int BatchID = (int)GridView1.DataKeys[GridView1.SelectedIndex].Value;
//Access Ingredients with batchID
var dtIngredients = new DataTable(); // create datasource with a query
GridView2.Visible = true;
GridView2.DataSource = dtIngredients;
GridView2.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.