locked
Show Gridview 1 Data In Gridview 2 with new Data RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my gridview1 Data if gridview conatin belongs to A then that row should show in Gridview2

    Item Name Qty Belongs To
    Apple 5 A
    Mango 4 C
    Orange 2 D

    Note My Gridview2 Show data from SQL Table + Grdiview1 data belongs to A show

    how to do using asp.net 2.0 C#

    Wednesday, February 21, 2018 8:48 AM

Answers

  • User-1838255255 posted

    Hi asp.ambur,

    According to your description and needs, I use datatable to replace the database, for more details, please check the following sample code: 

    <body>
        <form id="form1" runat="server">
            <div>
                <span>Table A</span>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" ItemStyle-Width="150" />
                        <asp:BoundField DataField="Qty" HeaderText="Qty" ItemStyle-Width="150" />
                        <asp:BoundField DataField="BelongsTo" HeaderText="BelongsTo" ItemStyle-Width="150" />
                    </Columns>
                </asp:GridView>
    
                <span>Table B</span>
                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" ItemStyle-Width="150" />
                        <asp:BoundField DataField="Qty" HeaderText="Qty" ItemStyle-Width="150" />
                        <asp:BoundField DataField="BelongsTo" HeaderText="BelongsTo" ItemStyle-Width="150" />
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    
    DataTable dt = new DataTable();
            protected void Page_Load(object sender, EventArgs e)
            {
                dt.Columns.Add("ItemName", typeof(string));
                dt.Columns.Add("Qty", typeof(string));
                dt.Columns.Add("BelongsTo", typeof(string));
    
                dt.Rows.Add("Apple", "5", "A");
                dt.Rows.Add("Mango", "4", "C");
                dt.Rows.Add("Orange", "2", "D");
    
                binddata();
            }
    
            public void binddata()
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
                DataRow[] result = dt.Select("BelongsTo= 'A'");
    
                DataTable table = new DataTable();
    
                table.Columns.Add("ItemName", typeof(string));
                table.Columns.Add("Qty", typeof(string));
                table.Columns.Add("BelongsTo", typeof(string));
                foreach (DataRow row in result)
                {
                    table.Rows.Add(row[0],row[1],row[2]);
                }
                GridView2.DataSource = table;
                GridView2.DataBind();
            }

    Result:

     

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 22, 2018 7:27 AM