C# 根据GridView1中选定的行填充GridView2

C# 根据GridView1中选定的行填充GridView2,c#,asp.net,C#,Asp.net,我的页面上有2个gridview:在gridview 1上,我有一个列,其中包含select链接、Id和名称,如下所示: Select | Id | Name select | 101 | Jack select | 102 | Cath Id | ProductID 101 | 111 101 | 222 101 | 333 现在,我要做的是,比如说,我单击了第一行中的select,即Jack,现在我的GridView2将显示Jack订购的产品,如下所示: Select | Id | Na

我的页面上有2个gridview:在gridview 1上,我有一个列,其中包含select链接、Id和名称,如下所示:

Select | Id | Name
select | 101 | Jack
select | 102 | Cath
Id | ProductID
101 | 111
101 | 222
101 | 333
现在,我要做的是,比如说,我单击了第一行中的select,即Jack,现在我的GridView2将显示Jack订购的产品,如下所示:

Select | Id | Name
select | 101 | Jack
select | 102 | Cath
Id | ProductID
101 | 111
101 | 222
101 | 333
如果我选择了Cath,GridView2将更改Cath订购的显示产品:

Id | productID
102 | 111
102 | 333
102 | 555

简而言之,我正在尝试根据GridView1中选定的行填充GridView2。我将asp.net与C一起使用。

当您在第一个网格中选择行时,然后在网格的selectIndexChanged事件中获取主键ID的值并存储在hiddenField中 如果您使用了Datakey,则使用

 SelectedDatakey
获取SelectedDatakey的值并存储在hiddenfield中

protected void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
  {

   hiddenfield1.value = CustomersGridView.SelectedDataKey.Value;

  }

现在,在填充第二个网格的查询中,传递第一个网格的键值,该键值已存储在where条件的hiddenfield中,并根据查询结果填充第二个网格

这是我的示例代码

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<Data> DataList = new List<Data>() { new Data { id = 1, id2 = 2 }, new Data { id = 3, id2 = 4 } };
        GridView1.DataSource = DataList;
        GridView1.DataBind();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow ) btn.NamingContainer;

    Label slNoLabel = (Label) row.FindControl("slNoLabel");
    // function to get data based on label vlue
    GridView2.DataSource=GetData(slNoLabel.Text);
    GridView2.DataBind();

}

DataTable GetData(string value)
{
DataTable tbl = new DataTable ();
    //   Calling DB 
    return tbl;
    }
}

public class Data
{
    public int id { get; set; }
    public int id2 { get; set; }
}
在用户界面中

 <div>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Select" OnClick="Button1_Click" Text="Select" />
                    <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:GridView ID="GridView2" runat="server"></asp:GridView>
</div>

您可以在为“选择”添加的链接按钮上添加onClick方法

   <asp:GridView ID="Gridview1" runat="server"
                    AllowPaging="true" PageSize="15" RowStyle-Wrap="true" EmptyDataRowStyle-ForeColor="Red"
                    AutoGenerateColumns="false" GridLines="None">
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                            <asp:LinkButton ID ="li_Select" runat="server" Text="Select" OnClick="li_Select_Click"></asp:LinkButton>                                   
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lbl_ID" Text='<%#Eval("ID") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lbl_Name" Text='<%#Eval("Name") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

                </asp:GridView>                  
现在,根据单击的行ID过滤数据或从数据库中获取数据

  protected void li_Select_Click(object sender, EventArgs e)
{

    LinkButton lnkbtn = (LinkButton)sender;
    GridViewRow row = (GridViewRow)lnkbtn.NamingContainer;
    Label lbl_ID = (Label)row.FindControl("lbl_ID");

  // You can fetch the data from the database on the basis of selected User ID .
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Product_ID");
    for (int i = 100; i <= 110; i++)
    {
        DataRow dr = dt.NewRow();
        dr["ID"] = i;
        dr["Product_ID"] = "P_" + i;
        dt.Rows.Add(dr);
    }
    // In this method you pass the id of the user, and datatable fetched from the    database of all products. or you can pass the id in the storedprocedure to get data of selected user only and then bind it here .
    GetData(lbl_ID.Text,dt);
}
protected void GetData(string ID, DataTable dt)
{       

    DataView dv = dt.DefaultView;
    dv.RowFilter = "ID=" + ID;
    Gridview2.DataSource = dv;
    Gridview2.DataBind();

}

在我看来,最好的方法是使用UpdatePanel绑定第二个网格,使用它你会有一个优势,没有回发它实际上是回发,但用户不会注意到它

如果您不使用UpdatePanel,那么在回发中绑定数据后,您将无法看到数据,除非您是通过Javascript完成的,这是一个难题……下面是实现该功能的示例代码:

ASPX页面:

现在解释一下,GridView2的UpdatePanel将在数据绑定到GridView2后更新它,这将显示新绑定的数据。GridView1的UpdatePanel用于防止链接按钮的回发


希望这能回答您的问题。

有两种方法可以做到这一点,1。您可以使用“更新”面板,使用它您将处于同一页面上。2.您可以单击select元素并重定向到新页面,在查询字符串中发送id,然后访问它并在下一页绑定网格视图。您可以使用javascript获取数据并手动将其绑定到网格2,也可以在同一页面上。从未尝试过此操作,但您可以在第二个gridview中添加EnableSortingAndPagingCallbacks=true,以防止刷新整个页面……首先,您的示例中有很多错误,其次,即使他成功地删除了错误,第二个网格将不会显示…@Hmxa moghal这对评论没有多大帮助。。。如果gridView上的选项工作正常,则应尝试在第二个gridView周围添加UpdatePanel,并在更新第二个grid view的数据后刷新它。