网格视图,其中包含每个项目的详细信息行

本文关键字:项目 详细信息 包含每 视图 网格 | 更新日期: 2023-09-27 18:33:15

我是 ASP.NET 新手。我有一个基本的网格视图,列出了公司名称(请参阅下面的代码)。我想做的是在每个公司下提供另一个联系人子列表,缩进在"子列表"中很少。

我想以某种方式循环遍历 Gridview 数据,然后运行联系人查询,将是"从Import_Contact中选择 *,其中公司 = '来自 GridView 数据源的公司'",然后在每个公司下显示如果联系人的子集。输出应如下所示:

 1) Company A
      1) Contact x
      2) Contact y
      1) Contact x
 2) Company B
      1) Contact h
      2) Contact i

等等...

<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="False" 
AllowPaging="False" 
SelectedIndex="0" 
DataSourceID="dsUploadList"                      
ShowHeaderWhenEmpty="True" EmptyDataText="No records Found"                        
EnableRowClick = "False" Width="100%" AllowSorting="True"  
AutoGenerateEditButton="False"               
>
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="Row"  ItemStyle-HorizontalAlign="Left" ItemStyle-Width="5%"  
    InsertVisible="False" DataField="Row"
    SortExpression="Row" />  
<asp:BoundField ReadOnly="True" HeaderText="Company"  ItemStyle-HorizontalAlign="Left" ItemStyle-Width="30%"  
    InsertVisible="False" DataField="Company"
    SortExpression="Company" />             
</Columns>      
</asp:GridView>
    <asp:SqlDataSource ID="dsUploadList" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnApps %>"
    SelectCommand="SELECT DISTINCT(Company) 
            , ROW_NUMBER() OVER (ORDER By Company ASC) AS Row
            FROM dbo.Import_CompanyContact icc WHERE RefNum = @RefNum "                    
> 
<SelectParameters> 
    <asp:QueryStringParameter  Name="RefNum" QueryStringField="RefNum" DefaultValue="0" Type="Int16" /> 
</SelectParameters>

关于如何实现这一目标的任何建议?代码的一个例子很棒。 谢谢。

网格视图,其中包含每个项目的详细信息行

我会改用ListViewDataList,因为它更容易操作显示。这应该会让你走上正确的道路:

<table width="595px">
    <asp:DataList BackColor="#ffffff" id="DataList1" DataKeyField="<ID>" OnItemDataBound="DataList1_ItemDataBound" runat="server" Width="100%">     
        <ItemTemplate>
           <tr>
              <td>
                  <asp:LinkButton ID="LinkButton1" runat="server" Text="+" OnCommand="LinkButton1_Command" CommandArgument='<%#Container.ItemIndex%>'></asp:LinkButton>    
              </td>
              <td><%#Eval("<COLUMN NAME>")%></td>
              <td><%#Eval("<COLUMN NAME>")%></td>
              <td><%#Eval("<COLUMN NAME>")%></td>
           </tr>
           <asp:Panel ID="pnlChildView" runat="server">
               <asp:DataList ID="DataList2" runat="server" Width="100%">
                   <ItemTemplate>
                       <tr>
                          <td><%#Eval("<CHILD OLUMN NAME>")%></td>
                          <td><%#Eval("<CHILD COLUMN NAME>")%></</td>
                          <td><%#Eval("<CHILD COLUMN NAME>")%></</td>                           
                       </tr>
                   </ItemTemplate>
               </asp:DataList>
           </asp:Panel>
        </ItemTemplate>
    </asp:DataList>
</table>

当用户单击 DataList1 中的 LinkButton/Button 时,执行以下操作:

protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    //pass index of item in command argument
    int itemIndex = Convert.ToInt32(e.CommandArgument);      
    //depending on your needs bind the details on demand
    //or preload during ItemDataBound 
    Panel childViewPanel = (Panel)DataList1.Items[itemIndex].FindControl("pnlChildView");
    if (childViewPanel != null)
    {
        //toggle visibility of childViewPanel and bind child list if panel is visible
        if (childViewPanel.Visible)
        {
            DataList childList = childViewPanel.FindControl("DataList2");
            if (childList != null)
            {
                int keyValue = (int)DataList1.DataKeys[itemIndex];
                //bind the list using DataList1 data key value
                childList.DataSource = <DATA SOURCE>; //get data using keyValue
                childList.DataBind();
            }  
        }
    }
}