简体   繁体   中英

how to get gridview focused row values into grid one by one

i have a master detail grid.when i focused on the master grid it wil displays the informationn detail grid.it shows one focus one grid at a time but want to keep the previous data in detail grid and second focused row data also in the grid which is folled by previous data like that

You can handle the SelectedIndexChanged event of the master grid and put datakey of the selected item to separate list. after that rebind details grid using all master grid data fir those id's stored in that list.

<asp:GridView ID="MasterGridView" runat="server" AutoGenerateColumns="false" Caption="Master"
        DataKeyNames="Id">
        <Columns>
            <asp:CommandField ShowSelectButton="true" SelectText="Select" />
            <asp:BoundField HeaderText="Id" DataField="Id" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
        </Columns>
    </asp:GridView>
    <hr />
    <asp:GridView ID="DetailsGridView" runat="server" AutoGenerateColumns="false" 
        DataKeyNames="Id" Caption="Details">
        <Columns>
            <asp:CommandField ShowSelectButton="true" SelectText="Select" />
            <asp:BoundField HeaderText="Id" DataField="Id" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
            <asp:BoundField HeaderText="Age" DataField="Age" />
        </Columns>
    </asp:GridView>

Code-behind:

    private List<MyClass> MasterGridViewDataSource
    {
        get { return Session["MasterGridViewDataSource"] as List<MyClass>; }
        set { Session["MasterGridViewDataSource"] = value; }
    }

    private List<int> SelectedIDs
    {
        get { return ViewState["SelectedIDs"] as List<int>; }
        set { ViewState["SelectedIDs"] = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        MasterGridView.SelectedIndexChanged += MasterGridView_OnSelectedIndexChanged;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var masterGridDataSource = GetMasterGridDataSource();
            MasterGridViewDataSource = masterGridDataSource;
            MasterGridView.DataSource = masterGridDataSource;
            MasterGridView.DataBind();

            SelectedIDs = new List<int>();
        }
    }

    private List<MyClass> GetMasterGridDataSource()
    {
        return (from item in Enumerable.Range(1, 10)
                select new MyClass { Id = item, Name = string.Format("Item #{0}", item), Age = item })
                .ToList();
    }

    void MasterGridView_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedId = (int)MasterGridView.SelectedDataKey.Value;
        if (!SelectedIDs.Contains(selectedId))
            SelectedIDs.Add(selectedId);

        DetailsGridView.DataSource = (from item in MasterGridViewDataSource
                                      join id in SelectedIDs on item.Id equals id
                                      select item);
        DetailsGridView.DataBind();
    }

    [Serializable]
    public class MyClass
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM