【问题标题】:GridViews in ASP.net; C#ASP.net 中的 GridView; C#
【发布时间】:2010-03-25 15:20:56
【问题描述】:

我正在使用 C# 和 ASP.net 创建网格视图...我需要帮助来创建 2 个具有相同 processID 的网格视图并从同一数据库中的不同 SQL 表中获取数据。当我单击第一行时第一个 gridview 它应该给我具有相同 processID 的文件列表及其在第二个 gridview 中的详细信息

一个进程 ID 中存储了许多不同的 xml 文件 例如第一个 gridview 中的 processID = 7A413EA4-8ECE-472D-92BE-F58C22E5C567 因此,在第二个表中具有相同 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>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    基本上你想创建一个主/从屏幕...这是作业吗?在任何情况下,您都希望将事件(选定的索引已更改)添加到第一个网格。然后在此处添加代码以设置数据源或“过滤”第二个网格中的数据,如果还没有,您也可以选择此时使第二个网格可见。

    第二个网格的数据源需要一个“WHERE”子句,它将数据源中的 processID 与第一个网格中选择的进程ID 进行比较。您可以将第一个网格的数据键设置为该字段(processID),​​以便于检索。

    祝你好运!

    【讨论】:

    • 不,这不是我的作业,我正在工作并且卡住了,因为我不习惯 c#.net 主要使用 java 工作
    • 酷!它更像是一个 j/k :)
    • 我真的需要帮助......因为我被卡住了,无法弄清楚该怎么做。这是我说的主/详细 grdview
    • 问题是第一个表中的 processId 被命名为 LOG_ProcessId 而在第二个表中 P2FV_ProcessId 怎么可以这样做
    【解决方案2】:

    你可以这样做:

    当您单击网格 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

    【讨论】:

      【解决方案3】:

      您应该能够通过标记来完成这一切。这是一个使用 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 中,使用 SQL where 子句绑定到主 GridView 中的选定主键:

      <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>
      

      另外,您需要在主 GridView 中设置 DataKeyNames 属性

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

      我认为您不需要设置详细 GridView 的 DataKeyNames。为了完整起见,您也可以在那里更改它。我使用 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

      【讨论】:

      • 在您的示例中,您在 mycase 中的两个表中都有 OrderID 我的意思是第一个表中的 processID 被命名为 LOG_ProcessId 而在第二个表中 P2FV_ProcessId 怎么可以这样做
      • 我无法通过我自己的数据库更改表中设置的字段的名称,因为该数据库用于公司其他人处理的不同其他程序中,除了修改sql
      • 无需更改您的字段名称。您需要用您自己的替换我在示例代码中显示的 SQL。我建议在 Visual Studio 的设计模式中为您的控件使用智能标记(无论如何我都这么称呼它)来配置控件。如果您想了解有关这方面的更多信息。我在我的答案中添加了一些链接。
      • 我尝试按照您所说的方式进行操作,这给了我一个服务器错误“/”应用程序中的服务器错误。对象必须实现 IConvertible。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.InvalidCastException:对象必须实现 IConvertible。
      • 源错误:在当前网络请求的执行过程中产生了一个未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多