Gridview Parent Child at ASP.net

I have two GridViews on an ASP.net 3.5 page. I have a HyperLink field as one of the fields in the first GridView.

When clicking on this hyperlink, I need to cause the second grid to be displayed, passing some values ​​to the showAllRecords method (the value from the hyperlink)

How to do it?

thank

+3
source share
3 answers

You can try TemplateField like this for GridView1 (main GridView)

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName"  CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

and in GridView1 RowCommand you can get a CommandArgument and set up a DataSource for GridView2 (a child GridView).

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName = "cmdName")
    {
        var arg = e.CommandArgument;

        // use arg to filter GridView2 DataSource
        GridView2.DataSource = FilteredDataSource;
        GridView2.DataBind();
        // show GridView2 if it hidden.
    }
}
+4
source

First you need to handle the SelectedIndexChanged event in the first grid, and then get the value from the hyperlink. Is the hyperlink DataKey? If so, you will get it GridOne.SelectedDataKey.Values["key"], otherwise you will get the actual cell valuefromGridOne = GridOne.SelectedRow.Cells[num].Text, where number is the cell number. After that, you can pass the value to the second grid by processing the selection of the objectDataSource event (provided that you use it to bind the data) and pass a value like thise.InputParameters["dataKey"] = valuefromGridOne;

0
source

All Articles