2010-03-25 40 views
0

我使用C#和ASP.net創建gridviews ...我需要幫助創建2個gridviews,他們有相同的processID,並從同一個數據庫中的不同SQL表獲取數據。當我點擊第一行第一個gridview應該給我在第二個gridview中具有相同processID及其細節的文件列表ASP.net中的GridViews; C#

一個進程ID有許多不同的xml文件存儲在其中 例如processID = 7A413EA4-8ECE-472D-92BE- F58C22E5C567第一gridview的 因此,所有的用戶名,日期,登錄其第二個表相同的ProcessID日的XML文件應該出現在第一表中選擇的行

這是我迄今所做的:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FS_PS2FS._Default" %> 

<!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>PS2FS Client</title> 
</head> 
<body bgcolor="lightgrey"> 
    <form id="form1" runat="server"> 
    <div> 
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; 
     <asp:Label ID="heading" runat="server" Font-Bold="True" Font-Names="Times New Roman" 
      Font-Size="XX-Large" Font-Underline="True" ForeColor="MidnightBlue" Text="PS2FS CLIENT" CssClass="normal" Height="41px" Width="227px" BorderStyle="Outset" BorderWidth="5px"></asp:Label><br /> 
     <br /> 
     <br /> 
     <asp:GridView ID="LOG_GridView" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
      CellPadding="4" DataSourceID="FS_PS2FS" ForeColor="#333333" Height="300px" Width="943px" HorizontalAlign="Center" CssClass="AlternatingRowStyle" PageSize="6" SelectedIndex="0" OnSelectedIndexChanging="LOG_GridView_SelectedIndexChanging" BorderColor="#999999"> 
      <FooterStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" CssClass="normal" /> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" CssClass="RowStyle" /> 
      <EditRowStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="normal" Font-Names="Cambria" BackColor="#999999" /> 
      <SelectedRowStyle BackColor="#FFFFC0" Font-Bold="True" ForeColor="#333333" BorderStyle="Outset" CssClass="SelectedRowStyle" BorderColor="Yellow" /> 
      <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" VerticalAlign="Middle" /> 
      <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="HeaderStyle" /> 
      <AlternatingRowStyle BackColor="White" HorizontalAlign="Center" ForeColor="#284775" /> 
      <Columns> 
       <asp:CommandField HeaderText="Select" ShowSelectButton="True" /> 
       <asp:BoundField DataField="LOG_ProcessId" HeaderText="Process ID" SortExpression="LOG_ProcessId" /> 
       <asp:BoundField DataField="LOG_Id" HeaderText="ID" InsertVisible="False" ReadOnly="True" 
        SortExpression="LOG_Id" /> 
       <asp:BoundField DataField="LOG_FileName" HeaderText="File Name" SortExpression="LOG_FileName" /> 
       <asp:BoundField DataField="LOG_Date" HeaderText="Date" SortExpression="LOG_Date" /> 
       <asp:BoundField DataField="LOG_Description" HeaderText="Description" SortExpression="LOG_Description" /> 
       <asp:BoundField DataField="LOG_UserId" HeaderText="User ID" SortExpression="LOG_UserId" /> 
      </Columns> 
     </asp:GridView> 
     &nbsp; 
     <asp:SqlDataSource ID="FS_PS2FS" runat="server" ConnectionString="<%$ ConnectionStrings:FreightsmartWebConnection %>" 
      SelectCommand="SELECT [LOG_Id], [LOG_FileName], [LOG_Description], [LOG_Date], [LOG_ProcessId], [LOG_Details], [LOG_UserId] FROM [FS_LOG_PS2FS] ORDER BY [LOG_Date] DESC"> 
     </asp:SqlDataSource> 
     <br /> 


</div> 
    <asp:SqlDataSource ID="PS2FS_FS" runat="server" ConnectionString="<%$ ConnectionStrings:FreightsmartWebConnection %>" 
     SelectCommand="SELECT [P2FV_ID], [P2FV_MessageType], [P2FV_MessageText], [P2FV_FileName], [P2FV_ProcessId], [P2FV_CreateDate], [P2FV_CreateUser] FROM [FS_LOG_PS2FS_Validation] ORDER BY [P2FV_CreateDate] DESC"> 
    </asp:SqlDataSource> 

    &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 
    <asp:GridView ID="LogValidation_GridView" runat="server" AutoGenerateColumns="False" CellPadding="4" 
     DataSourceID="PS2FS_FS" ForeColor="#333333" Height="300px" Width="940px" AllowPaging="True" HorizontalAlign="Center" PageSize="6" OnRowUpdated="LogValidation_GridView_RowUpdated" BorderColor="#999999"> 
     <FooterStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" CssClass="normal" /> 
     <Columns> 
      <asp:BoundField DataField="P2FV_ProcessId" HeaderText="Process ID" SortExpression="P2FV_ProcessId" Visible="False" /> 
      <asp:BoundField DataField="P2FV_ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" 
       SortExpression="P2FV_ID" /> 
      <asp:BoundField DataField="P2FV_MessageType" HeaderText="Message Type" SortExpression="P2FV_MessageType" /> 
      <asp:BoundField DataField="P2FV_MessageText" HeaderText="Message Text" SortExpression="P2FV_MessageText" /> 
      <asp:BoundField DataField="P2FV_FileName" HeaderText="File Name" SortExpression="P2FV_FileName" /> 
      <asp:BoundField DataField="P2FV_CreateDate" HeaderText="Create Date" SortExpression="P2FV_CreateDate" /> 
      <asp:BoundField DataField="P2FV_CreateUser" HeaderText="Create User" SortExpression="P2FV_CreateUser" /> 
     </Columns> 
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" BorderStyle="Outset" CssClass="normal" Wrap="True" /> 
     <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" /> 
     <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <EditRowStyle BackColor="#999999" /> 
    </asp:GridView> 

</form> 

回答

0

你可以做下列方式:

當您從網格線1在一行上點擊你應該觸發代碼隱藏文件的事件(看下面顯示的SelectedIndexChanged)。這裏面的事件,你應該查詢您的XML文件表發現,對電網1.選擇有了這樣一個查詢結果的行的相同的進程ID,你把它綁定到柵2

void LOG_GridView_SelectedIndexChanged(Object sender, EventArgs e) 
    { 
    // Get the currently selected row using the SelectedRow property. 
    GridViewRow row = CustomersGridView.SelectedRow; 

    // Do your logic here to query and databind to Grid 2... 
    } 

寫的那些這在你的GRID1聲明:

<asp:GridView ID="LOG_GridView" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
      CellPadding="4" DataSourceID="FS_PS2FS" ForeColor="#333333" Height="300px" Width="943px" HorizontalAlign="Center" CssClass="AlternatingRowStyle" PageSize="6" SelectedIndex="0" OnSelectedIndexChanging="LOG_GridView_SelectedIndexChanging" BorderColor="#999999" onselectedindexchanged="LOG_GridView_SelectedIndexChanged"> 

有關GridView控件的事件的更多信息,看看這些頁面在MSDN:

GridView..::.SelectedRow Property

GridView Class

0

基本上你想要創建一個主/細節屏幕......這是作業嗎?無論如何,你想添加一個事件(選擇索引改變)到第一個網格。然後在那裏添加代碼,可以設置數據源或者「過濾」第二個網格中的數據,如果還沒有,也可以選擇使第二個網格可見。

第二個網格的數據源需要一個「WHERE」子句,它將比較數據源中的processID與第一個網格中選定的那個。您可以將第一個網格的數據鍵設置爲該字段(processID)以便於檢索。

祝你好運!

+0

沒有它不是我的功課,我的工作,並就卡住不使用C#.NET IM大多是用java – sweetsecret 2010-03-25 15:46:59

+0

酷工作!這是更多的J/K :) – 2010-03-25 15:50:19

+0

我真的需要幫助它...因爲我卡住了,無法弄清楚該怎麼做。這是一個主/細節grdview我wud說 – sweetsecret 2010-03-25 16:51:20

0

你應該能夠從標記中完成這一切。下面是使用Northwind數據庫

定義主GridView的工作示例:

<asp:GridView ID="master" runat="server" DataSourceID="sdsMaster" 
     AutoGenerateColumns="False" DataKeyNames="OrderID" AllowPaging="True" > 
    <Columns> 
     <asp:CommandField ShowSelectButton="True" /> 
     <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" 
      ReadOnly="True" SortExpression="OrderID" /> 
     <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
      SortExpression="CustomerID" /> 
     <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
      SortExpression="EmployeeID" /> 
     <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" 
      SortExpression="OrderDate" /> 
     <asp:BoundField DataField="RequiredDate" HeaderText="RequiredDate" 
      SortExpression="RequiredDate" /> 
     <asp:BoundField DataField="ShippedDate" HeaderText="ShippedDate" 
      SortExpression="ShippedDate" /> 
     <asp:BoundField DataField="ShipVia" HeaderText="ShipVia" 
      SortExpression="ShipVia" /> 
     <asp:BoundField DataField="Freight" HeaderText="Freight" 
      SortExpression="Freight" /> 
    </Columns> 
    </asp:GridView> 

定義細節GridView控件:

<asp:GridView ID="detail" runat="server" DataSourceID="sdsDetail" 
     AutoGenerateColumns="False" DataKeyNames="OrderID,ProductID" > 
    <Columns> 
     <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" 
      SortExpression="OrderID" /> 
     <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
      SortExpression="ProductID" /> 
     <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
      SortExpression="UnitPrice" /> 
     <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
      SortExpression="Quantity" /> 
     <asp:BoundField DataField="Discount" HeaderText="Discount" 
      SortExpression="Discount" /> 
    </Columns> 
    </asp:GridView> 

這裏是主表中的SqlDataSource。只是基本的東西,選擇並顯示記錄:

<asp:SqlDataSource ID="sdsMaster" runat="server" 
     ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
     SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia], [Freight] FROM [Orders]" /> 

現在,在細節的SqlDataSource,使用where子句綁定到主GridView中選擇主鍵的SQL:

<asp:SqlDataSource ID="sdsDetail" runat="server" 
     ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
     SelectCommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity], [Discount] FROM [Order Details] WHERE ([OrderID] = @OrderID)" > 
    <SelectParameters> 
     <asp:ControlParameter ControlID="master" DefaultValue="0" Name="OrderID" 
      PropertyName="SelectedValue" Type="Int32" /> 
    </SelectParameters> 
    </asp:SqlDataSource> 

通知的SelectParameters和使用ControlParameter在主GridView中查找所選記錄的數據鍵。

編輯 由於您正在使用您自己的數據庫與不同的字段名稱,然後相應地修改SQL。

FROM [FS_LOG_PS2FS_Validation] WHERE ([P2FV_ProcessId] = @LOG_ProcessId) 

,改變SelectParameter

<SelectParameters> 
    <asp:ControlParameter ControlID="master" DefaultValue="0" Name="LOG_ProcessId" 
     PropertyName="SelectedValue" Type="Int32" /> 
</SelectParameters> 

此外,您將需要設置的DataKeyNames在主GridView控件屬性

<asp:GridView ID="master" runat="server" DataSourceID="sdsMaster" 
      AutoGenerateColumns="False" DataKeyNames="LOG_ProcessID" AllowPaging="True" > 
     <Columns> 

我不認爲你需要設置的DataKeyNames的細節GridView。爲了完整性,您也可以在那裏更改它。我在Visual Studio 2008設計視圖中使用智能標記來生成示例代碼。讓我知道如果這不明確。

編輯2 以下是描述配置ASP.NET數據控件的一些鏈接。

Master/Detail Using a Selectable Master GridView with a Details DetailView

Querying Data with the SqlDataSource Control

Data Access Tutorials

+0

在你的例子中,你有兩個表中的OrderID mycase我的意思是在第一個表中的問題processID被命名爲LOG_ProcessId,並在第二個表P2FV_ProcessId如何能這樣做 – sweetsecret 2010-03-25 17:37:02

+0

我已經更新了我的答案。我希望這使事情更清楚。 – DaveB 2010-03-25 17:58:22

+0

讓我試試,如果它幫助非常感謝 – sweetsecret 2010-03-25 19:17:38