簡體   English   中英

如何將選定的行從網格視圖綁定到另一個網格視圖

[英]How to Bind Selected Rows From Grid View to Another Grid View

我將選擇的行從gridview添加到另一個gridview。 有人可以幫我嗎? 當我嘗試將gridview綁定到另一個gridview時,沒有出現另一個gridview。

我在下面發布的aspx文件是關於如何創建gridview的。 gridview1將從數據庫表中獲取其gridview內容。 雖然CS文件是關於如何將選定的行從gridview1綁定到gridview2的。 現在的問題是,當我嘗試將行綁定到上方時,未顯示gridview2,僅出現了gridview1。

這是我的代碼:

ASPX文件:

<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文件:

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();
        }
    }
}

任何幫助是極大的贊賞。 謝謝!

我真的不明白您所做的凌亂代碼(您的問題中遺漏了太多東西)。 請參閱下面的鏈接,這些鏈接將說明您的示例。

更新:

dt.Rows.Add()必須在for循環內,因此每次它將添加新行。

嘗試更新cs文件中的以下代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM