如何将选定的行从网格视图绑定到另一个网格视图 [英] How to Bind Selected Rows From Grid View to Another Grid View

查看:63
本文介绍了如何将选定的行从网格视图绑定到另一个网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将gridview中选定的行添加到另一个gridview中。有人可以帮我吗?当我尝试将gridview绑定到另一个gridview时,不会出现另一个gridview。



这是我的代码:

Aspx文件:

Hi, 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.

This is my codes:
Aspx file:

<h3 class="h3">Grid View1</h3>
            <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:



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!


Any help is greatly appreciated. Thanks!

推荐答案

So, for each row, the RowdataBound is called and each time you are binding the other GridView, which is a very wrong approach.



You should, ideally, after the first GridView Loaded and rows are selected, execute block of code which loops through the Rows of first GridView and copy that row to second GridView.



After the loop, bind the second GridView.
So, for each row, the RowdataBound is called and each time you are binding the other GridView, which is a very wrong approach.

You should, ideally, after the first GridView Loaded and rows are selected, execute block of code which loops through the Rows of first GridView and copy that row to second GridView.

After the loop, bind the second GridView.


这篇关于如何将选定的行从网格视图绑定到另一个网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆