gpt4 book ai didi

c# - 带有每个项目的详细信息行的 Gridview

转载 作者:行者123 更新时间:2023-11-30 20:54:29 25 4
gpt4 key购买 nike

我是 ASP.NET 的新手。我有一个基本的工作 gridview 列出公司名称(见下面的代码)。我想做的是在每个公司下提供另一个联系人子列表,在“子列表”中缩进一点。

我想以某种方式遍历 Gridview 数据,然后运行联系人查询,将是“Select * from Import_Contact where Company = 'The Company from the GridView DataSource'”,然后在每个公司下显示联系人子集。输出应如下所示:

 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>

关于如何完成此任务的任何建议? be great 的代码示例。谢谢。

最佳答案

我会使用 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 中的 LinkBut​​ton/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();
}
}
}
}

关于c# - 带有每个项目的详细信息行的 Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com