简体   繁体   中英

How to Bind Selected Rows From Grid View to Another Grid View

I would to add the selected rows from gridview into another gridview. Could someone help me on this? When I try to bind gridview to another gridview, another gridview doesn't appear.

The aspx file I posted below is about how I created the gridview. The gridview1 will get its gridview contents from the database table. While the cs file is about how I am going to bind selected rows from gridview1 into gridview2. The problem now is when I try to bind the rows over, the gridview2 didn't appear, only gridview1 appeared.

This is my codes:

ASPX file:

<h3 class="h3">Grid View1</h3>
<div style="width: 100%; height: 400px; overflow: auto">
    <asp:GridView ID="GridView1"
                  runat="server"
                  AllowSorting="True"
                  AutoGenerateColumns="False"
                  Width="100%"
                  CellPadding="6"
                  ForeColor="#333333"
                  GridLines="Horizontal"
                  BorderColor="Black"
                  BorderStyle="Solid"
                  BorderWidth="2px"
                  EmptyDataText="Record Not Found"
                  OnRowDataBound="GridView1_OnRowDataBound">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="DATE"
                            HeaderText="DATE"></asp:BoundField>
            <asp:BoundField DataField="CODE"
                            HeaderText="CODE"></asp:BoundField>
            <asp:BoundField DataField="PROFILE_NAME"
                            HeaderText="PROFILE_NAME"></asp:BoundField>
            <asp:BoundField DataField="DESCRIPTION"
                            HeaderText="DESCRIPTION"></asp:BoundField>
            <asp:BoundField DataField="STATUS"
                            HeaderText="STATUS"></asp:BoundField>
            <asp:BoundField DataField="USER"
                            HeaderText="USER"></asp:BoundField>
            <asp:BoundField DataField="SUB_USER"
                            HeaderText="SUB_USER"></asp:BoundField>
            <asp:BoundField DataField="SCORE"
                            HeaderText="SCORE"></asp:BoundField>
            <asp:BoundField DataField="ROLE"
                            HeaderText="ROLE"></asp:BoundField>
            <asp:BoundField DataField="QUANTITY"
                            HeaderText="QUANTITY"></asp:BoundField>
            <asp:BoundField DataField="ITEM"
                            HeaderText="ITEM"></asp:BoundField>
            <asp:BoundField DataField="PRICE"
                            HeaderText="PRICE"></asp:BoundField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1"
                     Font-Bold="False"
                     ForeColor="Black" />
        <HeaderStyle BackColor="#507CD1"
                     Font-Bold="False"
                     ForeColor="Black"
                     BorderStyle="Solid"
                     BorderWidth="2px" />
        <PagerStyle BackColor="#2461BF"
                    ForeColor="White"
                    HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1"
                          Font-Bold="False"
                          ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <h3 class="h3">Grid View2</h3>
    <div style="width: 100%; height: 400px; overflow: auto">
        <asp:GridView ID="GridView2"
                      runat="server"
                      AllowSorting="True"
                      AutoGenerateColumns="False"
                      Width="100%"
                      CellPadding="6"
                      ForeColor="#333333"
                      GridLines="Horizontal"
                      BorderColor="Black"
                      BorderStyle="Solid"
                      BorderWidth="2px"
                      EmptyDataText="Record Not Found"
                      OnRowDataBound="GridView2_OnRowDataBound">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="DATE"
                                HeaderText="DATE"></asp:BoundField>
                <asp:BoundField DataField="CODE"
                                HeaderText="CODE"></asp:BoundField>
                <asp:BoundField DataField="PROFILE_NAME"
                                HeaderText="PROFILE_NAME"></asp:BoundField>
                <asp:BoundField DataField="DESCRIPTION"
                                HeaderText="DESCRIPTION"></asp:BoundField>
                <asp:BoundField DataField="STATUS"
                                HeaderText="STATUS"></asp:BoundField>
                <asp:BoundField DataField="USER"
                                HeaderText="USER"></asp:BoundField>
                <asp:BoundField DataField="SUB_USER"
                                HeaderText="SUB_USER"></asp:BoundField>
                <asp:BoundField DataField="SCORE"
                                HeaderText="SCORE"></asp:BoundField>
                <asp:BoundField DataField="ROLE"
                                HeaderText="ROLE"></asp:BoundField>
                <asp:BoundField DataField="QUANTITY"
                                HeaderText="QUANTITY"></asp:BoundField>
                <asp:BoundField DataField="ITEM"
                                HeaderText="ITEM"></asp:BoundField>
                <asp:BoundField DataField="PRICE"
                                HeaderText="PRICE"></asp:BoundField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1"
                         Font-Bold="False"
                         ForeColor="Black" />
            <HeaderStyle BackColor="#507CD1"
                         Font-Bold="False"
                         ForeColor="Black"
                         BorderStyle="Solid"
                         BorderWidth="2px" />
            <PagerStyle BackColor="#2461BF"
                        ForeColor="White"
                        HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1"
                              Font-Bold="False"
                              ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

CS file:

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs     e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, “USER”)) == “ALAN TAN”)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(“DATE”);
            dt.Columns.Add(“CODE”);
            dt.Columns.Add(“PROFILE_NAME”);

            dt.Columns.Add(“DESCRIPTION”);
            dt.Columns.Add(“STATUS”);
            dt.Columns.Add(“USER”);
            dt.Columns.Add(“SUB_USER”);
            dt.Columns.Add(“SCORE”);
            dt.Columns.Add(“ROLE”);
            dt.Columns.Add(“QUANTITY”);
            dt.Columns.Add(“ITEM”);
            dt.Columns.Add(“PRICE”);
            DataRow dataRow;
            dataRow = dt.NewRow();
            int i2 = 1;
            for(int i=0; i<dataRow.Table.Columns.Count; i++)
            {
                dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
                i2++;
            }
            dt.Rows.Add(dataRow);
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }
    }
}

Any help is greatly appreciated. Thanks!

I Really didn't understand messy code done by you (Too many things are missing in your question). Refer below links that will explain you will example.

Updates:

dt.Rows.Add() must be inside of for loop so everytime it will get add new row.

try to update following code in cs file

for(int i=0; i<dataRow.Table.Columns.Count; i++)
{ 
   dt.Rows.Add(GridView1.SelectedRow.Cells[i2].Text);
   i2++;
}
GridView2.DataSource = dt;
GridView2.DataBind();

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