0

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>
&nbsp;

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

1 Answer 1

1

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?
2
  • postimage.org/image/2xj9bdles take a look at this screen shot. This how I want the page to look like. So i want the girdview2 (i.e., child gridview) to appear right below each row of "gridview1" i.e., the parent gridview. I don't know how to approach this problem. I have been struggling with this for some time. Please help me Commented Mar 30, 2011 at 8:57
  • 1
    Right. You don't need nested gridview for this. I am going to amend my answer as soon as I can to present an alternative. Meanwhile look at TemplateFields in a gridview. You want to have a table as a templatefield, then in Gridview's row data bound you want to dynamically create rows in the table in this row to display nested data. Or you can use a repeater control
    – Nikhil
    Commented Mar 31, 2011 at 11:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.