• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Populate grid from selected row of another grid

I have JOBS which have many VERSIONS, which have many PACKAGES.  I'm building a utility where I can enter JOB NUMBER, which will populate a grid with all VERSIONs for the JOB.  I want to then click a VERSION description link, which will populate another grid with the PACKAGES.

So basically,
- Enter job#, click enter to fill Grid1 with VERSIONS. (Display VersionID, Version_Description)
- Click Grid1.Version_Description to fill Grid2 with PACKAGES (Display PackageID, Package_Description)

I can't seem to find a simple example of this.  Please advise.
0
JT_SIRO
Asked:
JT_SIRO
  • 3
1 Solution
 
ddayx10Commented:
Something like this should give you the idea.
<asp:GridView ID="GridOuter" runat="server" DataSourceID="SqlDSGridOuter" OnRowCommand="GridOuter_RowCommand">
	<Columns>
		<asp:TemplateField>
			<ItemTemplate>
				<asp:Button ID="Btn1" runat="server" Text="Fill Inner Grid"
				 CommandArgument='<%# Bind("ItemNumber") %>' />
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField>
			<ItemTemplate>
				<asp:GridView ID="GridInner" runat="server" />
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDSGridOuter" runat="server"
 ConnectionString='<%$ ConnectionStrings:DevTesting %>'
 SelectCommand="SELECT * FROM ITEMS" />

Open in new window

protected void GridOuter_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        //find the current row and use that to find the inner gridview
        GridViewRow gRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        GridView GridInner = (GridView)gRow.FindControl("GridInner");

        //get the button to utilize its commandarg later
        Button Btn1 = (Button)e.CommandSource;

        //Get data for inner/nested grid and bind to it
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DevTesting"].ToString());
        SqlCommand cmd = new SqlCommand("SELECT * FROM Items2 WHERE ItemID = @ItemId", conn);
        cmd.Parameters.Add(new SqlParameter("@ItemId", Convert.ToInt32(Btn1.CommandArgument)));
        using (conn)
        {
            conn.Open();
            GridInner.DataSource = cmd.ExecuteReader();
            GridInner.DataBind();
        }
    }

Open in new window

0
 
ddayx10Commented:
BTW. For such a specific requirement that you couldn't find a link to easily this was worth more than 250 pts.
0
 
JT_SIROAuthor Commented:
OK, sorry.  I added points to this post.

I think this solution will work, but the button clicks is not firing the GridOuter_RowCommand event.  It builds without errors.  The outer grid loads fine, but my break point is never hit in GridOuter_RowCommand when I click a button from within the outer grid.  Do I need to add an event in the button or something?  Thanks -
0
 
ddayx10Commented:
This is what calls establishes the event.

<asp:GridView ID="GridOuter" runat="server" DataSourceID="SqlDSGridOuter" OnRowCommand="GridOuter_RowCommand">

If you have a button somewhere in the gridview it should cause that event to fire:

<templatefield><itemtemplate><asp:button id="btn" runat="server" text="MyButton" /></itemtemplate></templatefield>

OR:

<asp:buttonfield buttontype="Button"
            commandname="Select"
            headertext="Select Customer"
            text="Select"/>

If you're syntax is correct then try to show me some code (the gridview and the rowcommand) and I'll have a look :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now