gridview中的数据未复制到另一个gridview [英] Data from a gridview didn't copy to another gridview

查看:64
本文介绍了gridview中的数据未复制到另一个gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图通过使用复选框将数据行从一个gridview复制到另一个gridview。因此,只要在gridview1上检查了一行,它就会自动添加到gridview2中,无论何时取消选中,都会将其删除。我确实找到了一些示例代码,并根据我的要求进行调整,但似乎没有正常工作。当我检查一行时,页面确实刷新了,但它没有添加到gridview2。以下是我的代码:



我的主要网格视图:

Hi guys,
I am trying to copy rows of data from one gridview to another gridview by using a checkbox. So whenever a row is checked on gridview1, it will automatically be added to gridview2 and whenever it is unchecked, it will be removed. I did find some sample code and I adjust it to my requirements but it doesn't seem to be working properly. When I checked a row, the page did refresh but it is not added to gridview2. Below is my code:

my primary gridview:

<asp:GridView ID="PrimaryGrid" runat="server" AutoGenerateColumns="False" 

        BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 

        CellPadding="3" CellSpacing="2" DataKeyNames="SubActivity" 

        DataSourceID="SqlDataSource2" EmptyDataText="No data selected">
  
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this)" AutoPostBack="true" OnCheckedChanged="CheckBox_CheckChanged" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="check" runat="server" onclick="Check_click(this)" AutoPostBack="true" OnCheckedChange="CheckBox_CheckChanged" />
                </ItemTemplate>
            </asp:TemplateField>

                <asp:BoundField DataField="ActivityID" HeaderText="ActivityID" SortExpression="ActivityID" HtmlEncode="false"/>
                <asp:BoundField DataField="SubActivity" HeaderText="SubActivity" SortExpression="SubActivity" HtmlEncode="false"/>
                <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" HtmlEncode="false"/>
        </Columns>
        
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FFF1D4" />
        <SortedAscendingHeaderStyle BackColor="#B95C30" />
        <SortedDescendingCellStyle BackColor="#F1E5CE" />
        <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>





我的次要网格视图:



my secondary gridview:

<div style="removed: absolute; z-index: inherit; removed 185px; removed 765px; height: 215px; width: 249px;">
        <asp:GridView ID="FinalReport" runat="server" AutoGenerateColumns="False" 

            EmptyDataText="No records selected" BackColor="LightGoldenrodYellow" 

            BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" 

            GridLines="None">
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
            <Columns>
                <asp:BoundField DataField="RouteID" HeaderText="Route ID" />
                <asp:BoundField DataField="ActivityID" HeaderText="Activity ID" />
                <asp:BoundField DataField="Budget" HeaderText="Budget" />
            </Columns>
            <FooterStyle BackColor="Tan" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 

                HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <SortedAscendingCellStyle BackColor="#FAFAE7" />
            <SortedAscendingHeaderStyle BackColor="#DAC09E" />
            <SortedDescendingCellStyle BackColor="#E1DB9C" />
            <SortedDescendingHeaderStyle BackColor="#C2A47B" />
        </asp:GridView>
    </div>





这是我获取数据并将其存储到数据表中时的代码将其保存到viewstate:



This is my code to when get the data and store it into a datatable saves it into a viewstate:

private void GetData()
        {
            DataTable dt = new DataTable();
            if (ViewState["SelectedRecords"] != null)
                dt = (DataTable)ViewState["SelectedRecords"];
            else
                dt = CreateDataTable();
            CheckBox checkAll = (CheckBox)PrimaryGrid.HeaderRow
                                .Cells[0].FindControl("checkAll");
            for (int i = 0; i < PrimaryGrid.Rows.Count; i++)
            {
                if (checkAll.Checked)
                {
                    dt = AddRow(PrimaryGrid.Rows[i], dt);
                }
                else
                {
                    CheckBox check = (CheckBox)PrimaryGrid.Rows[i]
                                    .Cells[0].FindControl("check");
                    if (check.Checked)
                    {
                        dt = AddRow(IntelToIntelDistiGrid.Rows[i], dt);
                    }
                    else
                    {
                        dt = RemoveRow(PrimaryRecord.Rows[i], dt);
                    }
                }
            }
            ViewState["SelectedRecords"] = dt;
        }





这是设置数据的代码:



This is the code for setting the data:

private void SetData()
        {
            CheckBox checkAll = (CheckBox)PrimaryRecord.HeaderRow.Cells[0].FindControl("checkAll");
            checkAll.Checked = true;
            if (ViewState["SelectedRecords"] != null)
            {
                DataTable dt = (DataTable)ViewState["SelectedRecords"];
                for (int i = 0; i < PrimaryGrid.Rows.Count; i++)
                {
                    CheckBox check = (CheckBox)PrimaryGrid.Rows[i].Cells[0].FindControl("check");
                    if (check != null)
                    {
                        DataRow[] dr = dt.Select("RouteID = '" + PrimaryGrid.Rows[i].Cells[1].Text + "'");
                        check.Checked = dr.Length > 0;
                        if (!check.Checked)
                        {
                            checkAll.Checked = false;
                        }
                    }
                }
            }
        }





这个是选中复选框时触发的事件:



and this is the event fired when the checkbox is checked:

protected void CheckBox_CheckChanged(object sender, EventArgs e)
        {
            GetData();
            SetData();
            BindPrimaryGrid();
            BindSecondaryGrid();
        }





我在选中复选框时使用的javascript:



javascript that i use when a checkbox is checked:

function Check_click(objRef) 
    {
        //Get the Row based on checkbox
        var row = objRef.parentNode.parentNode;

        //Get the reference of GridView
        var GridView = row.parentNode;

        //Get all input elements in Gridview
        var inputList = GridView.getElementsByTagName("input");

        for (var i = 0; i < inputList.length; i++) 
        {
            //The First element is the Header Checkbox
            var headerCheckBox = inputList[0];

            //Based on all or none checkboxes
            //are checked check/uncheck Header Checkbox
            var checked = true;
            if (inputList[i].type == "chckbox" && inputList[i] != headerCheckBox) 
            {
                if (!inputList[i].checked) 
                {
                    checked = false;
                    break;
                }
            }
        }
        headerCheckBox.checked = checked;
    }





javascript i used if all the checkbox is ticked:



javascript i used if all the checkbox is ticked:

function checkAll(objRef) 
    {   
        var GridView = objRef.parentNode.parentNode.parentNode;
        var inputList = GridView.getElementsByTagName("input");
        for (var i = 0; i < inputList.length; i++) {
            var row = inputList[i].parentNode.parentNode;
            if (inputList[i].type == "checkbox" && objRef != inputList[i]) 
            {
                if (objRef.checked) 
                {
                    inputList[i].checked = true;
                }
                else 
                {
                    if (row.rowIndex % 2 == 0) 
                    {
                        row.style.backgroundColor = "#C2D69B";
                    }
                    else 
                    {
                        row.style.backgroundColor = "white";
                    }
                    inputList[i].checked = false;
                }
            }
        }
    }





add row function:



add row function:

private DataTable AddRow(GridViewRow gvRow, DataTable dt)
        {
            DataRow[] dr = dt.Select("RouteID= '" + gvRow.Cells[1].Text + "'");
            if (dr.Length <= 0)
            {
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count - 1]["RouteID"] = gvRow.Cells[1].Text;
                dt.Rows[dt.Rows.Count - 1]["ActivityID"] = gvRow.Cells[2].Text;
                dt.Rows[dt.Rows.Count - 1]["Budget"] = gvRow.Cells[3].Text;
                dt.AcceptChanges();
            }
            return dt;
        }





remove row function:



remove row function:

private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
        {
            DataRow[] dr = dt.Select("RouteID = '" + gvRow.Cells[1].Text + "'");
            if (dr.Length > 0)
            {
                dt.Rows.Remove(dr[0]);
                dt.AcceptChanges();
            }
            return dt;
        }



there’s a few more functions left but i think this 3 is the main one.

p/s : sorry for the long post.


there's a few more functions left but i think this 3 is the main one.
p/s : sorry for the long post.

推荐答案

Once you can refer this



transfer-selected-rows-from-one-gridview-to-another-in-asp.net.aspx[^]



move-selected-gridview-rows-to-another.html[^]



Hope it will help you..
Once you can refer this

transfer-selected-rows-from-one-gridview-to-another-in-asp.net.aspx[^]

move-selected-gridview-rows-to-another.html[^]

Hope it will help you..


one easy way is there



fetch all data of gridview1 in dataset then gridview2.datasource = dataset;


$ b$b its easy just think
one easy way is there

fetch all data of gridview1 in dataset then gridview2.datasource = dataset;

its easy just think


这篇关于gridview中的数据未复制到另一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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