嵌套gridview的问题

发布于 2024-10-27 21:54:49 字数 2659 浏览 0 评论 0原文

我正在尝试动态添加一个 gridview (让我们称之为 gridview2)到通过 .aspx 页面添加的现有 gridview (让我们称之为 gridview1)。

我想做的是:根据“gridview1”中显示的行内容(更具体地说,单元格 1 中显示的发票编号,基于零的索引),我从列表中过滤数据并在中显示数据“网格视图2”。

我在某个地方犯了一些可怕的错误,或者我的方法应该从根本上是错误的。

以下是在 .aspx 页面中添加的 gridview1 的代码。

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="ClickingBtn_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="Date Of Transaction" 
            HeaderText="Date Of Transaction" SortExpression="Date Of Transaction" />
        <asp:BoundField DataField="Invoice Number" HeaderText="Invoice Number" 
            SortExpression="Invoice Number" />
        <asp:BoundField DataField="totalAmount" HeaderText="totalAmount" 
            ReadOnly="True" SortExpression="totalAmount" />
    </Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ComponentDBConnectionString %>" 
    SelectCommand="SelectUserPreviousHistory" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:Parameter DefaultValue="duran" Name="userName" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
&nbsp;

这是获取数据,如下所示: 此处

然后我尝试添加另一个 gridview,具体取决于通过以下代码在每行中显示唯一的发票号码:

foreach (GridViewRow gridviewrow in GridView1.Rows)
        {

            GridView gridView2 = new GridView();
            gridView2.AutoGenerateColumns = true;
            String x = gridviewrow.Cells[1].Text; //IT FETCHES THE INVOICE NUMBER FROM EACH ROW
            softwareTitlesList = SoftwareListRetrieve(); //lIST OF ALL THE SOFTWARE TITLES ADDED TO LIST
            ArrayList titles = new ArrayList();
            foreach (SoftwareTitles softwareTitle in softwareTitlesList)
            {
                if (softwareTitle.InvoiceNumber.Contains(x))
                    titles.Add(softwareTitle.SoftwareTitle); //ADDING ONLY THOSE TITLES THAT MATCH THAT PARTICULAR INVOICE NUMBER
            }
            gridView2.DataSource = titles;
            gridView2.DataBind();
        }

但似乎没有发生任何事情。这有什么问题吗?

请帮助我

顺便说一句,我正在使用 asp.net/c# Visualstudio 2010。而且我在我的项目中没有使用 LINQ,数据库是 sql server 2005

感谢您的期待

I'm trying add a gridview dynamically (lets call this gridview2) to existing gridview (lets call this gridview1) added through .aspx page.

What i'm trying to do is: Depending upon the content of the row (more specifically invoice number displayed in the cell-1 with zero based indexing) displayed in "gridview1" i'm filtering data from a list and displaying the data in "gridview2".

I'm doing some terrible mistake somewhere or my approach should be fundamentally wrong.

Below is the code for gridview1 added in .aspx page.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="ClickingBtn_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="Date Of Transaction" 
            HeaderText="Date Of Transaction" SortExpression="Date Of Transaction" />
        <asp:BoundField DataField="Invoice Number" HeaderText="Invoice Number" 
            SortExpression="Invoice Number" />
        <asp:BoundField DataField="totalAmount" HeaderText="totalAmount" 
            ReadOnly="True" SortExpression="totalAmount" />
    </Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ComponentDBConnectionString %>" 
    SelectCommand="SelectUserPreviousHistory" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:Parameter DefaultValue="duran" Name="userName" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
 

This is fetching the data as shown here: here

And then I'm trying to add another gridview depending upon the unique invoice number displayed in each row by following code:

foreach (GridViewRow gridviewrow in GridView1.Rows)
        {

            GridView gridView2 = new GridView();
            gridView2.AutoGenerateColumns = true;
            String x = gridviewrow.Cells[1].Text; //IT FETCHES THE INVOICE NUMBER FROM EACH ROW
            softwareTitlesList = SoftwareListRetrieve(); //lIST OF ALL THE SOFTWARE TITLES ADDED TO LIST
            ArrayList titles = new ArrayList();
            foreach (SoftwareTitles softwareTitle in softwareTitlesList)
            {
                if (softwareTitle.InvoiceNumber.Contains(x))
                    titles.Add(softwareTitle.SoftwareTitle); //ADDING ONLY THOSE TITLES THAT MATCH THAT PARTICULAR INVOICE NUMBER
            }
            gridView2.DataSource = titles;
            gridView2.DataBind();
        }

But nothing seems to be happening. What is wrong with this ?

Please help me

BTW I'm using asp.net/c# visualstudio 2010. And i'm not using LINQ in my project and the database is sql server 2005

Thanks in anticipation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月松间行 2024-11-03 21:54:49

您的方法存在很多问题。

  1. 您确实在服务器端代码中创建了一个新的 gridview,但您从未将 gridview 添加到页面或页面上的任何其他控件!它有效地告诉 .NET 创建一个 gridview,将其绑定到数据,但它并没有告诉 .NET 在页面上的“何处”呈现 gridview。因此,您看不到任何内容。

  2. 动态控件 - 如果我在这里很自以为是,我很抱歉,但我认为您还没有太多地使用 ASP.NET 中的动态控件。我衷心的建议是尽可能不要使用它们。动态创建的控件在其状态、回发事件方面具有巨大的负担,并且实际上必须在每次回发到页面时添加。简而言之,如果您在页面上动态添加一个按钮,如

    按钮 btn = new Button();

    btn.Text = "嗨";
    Page.Controls.Add(btn);

然后,您必须在页面的每次回发时触发此代码。如果在任何回发期间此代码没有被触发,则当客户端看到该页面时该按钮将丢失。

我可以建议一些方法来实现您想要做的事情,但是为了能够做到这一点,我想知道

  1. 您希望第二个 gridview 出现在页面中的位置(即嵌入第一个 gridview 内或外部)
  2. 你想什么时候填充第二个gridview?是不是类似于用户在第一个gridview中点击一行,然后在第二个gridview中看到相关信息的情况?

There are quite a few problems with your approach.

  1. You do create a new gridview in your server side code but you never add the gridview to the page or any other control on the page! It is effectively telling the .NET to create a gridview, bind it to the data but it is not telling .NET "where" to render the gridview on the page. Hence you don't see anything.

  2. Dynamic controls - I am sorry if I am being presumptuous here but I think you haven't had much play with dynamic controls within ASP.NET. My whole hearted advice will be to not use them as far as you can. Dynamically created controls come with a HUGE baggage with respect to their state, their post back events and actually have to be added on every post back to the page. In a nutshell, if you add a button dynamically on the page as

    Button btn = new Button();

    btn.Text = "hi";
    Page.Controls.Add(btn);

Then you must fire this code on every postback of the page. If during any postback this code doesn't get fired, the button will be missing when the client sees the page.

I can suggest some approaches to achieve what you are trying to do, however to be able to do that I would like to know

  1. Where do you want the second gridview to appear in the page (i.e. embedded within the first one or outside of it)
  2. When do you want to populate the second gridview? Is it something like the situation that the user clicks one row in the first gridview and then sees the relevant information in the second gridview?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文