如何将一个gridview行转移到asp.net中的另一个gridview行

时间:2016-04-12 06:43:34

标签: c# asp.net gridview

我有两个网格视图:

  1. gvdetails
  2. gvTranferRows
  3. gvdetails与所选数据库绑定。字段是:

    • id

    • moduleName

    • Item字段。

    gvTransferRows字段中:

    • id

    • ModuleName

    • Item

    • BatchNo

    BatchNodropdownlist中选择。

    但我想将选定的行从gvdetails转移到gvTranferRows

    在此过程中,我通过同时点击删除按钮成功删除了gvdetails个选定的行并将其添加到gvTranferRows,反之亦然。

    但点击删除按钮完成后再次执行相同的操作,第二次点击添加按钮,gvdetails gridview将完全删除它转移到gvTransferRows

    我需要的是在添加按钮中选择第二次,它不会仅删除选定的值删除并转移到第二个gridview。

    这是我为代码尝试的代码:

    的.aspx:

      <table align="center">
                    <tr>
                        <td class="auto-style2"></td>
                        <td class="auto-style1">
                            <asp:DropDownList ID="ddlbatchno" runat="server" Height="16px" Width="131px">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style2"></td>
                    </tr>
                    <tr>
                        <td class="auto-style2" valign="top">
                            <asp:GridView ID="gvDetails" runat="server" DataKeyNames="ModuleName" AutoGenerateColumns="false" CellPadding="5">
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="id" HeaderText="id" />
                                    <asp:BoundField DataField="ModuleName" HeaderText="ModuleName" />
                                    <asp:BoundField DataField="Item" HeaderText="Item" />
                                      <asp:BoundField DataField="BatchNo" HeaderText="BatchNo" />
    
                                </Columns>
                                <HeaderStyle BackColor="#6699ff" Font-Bold="true" ForeColor="White" />
                            </asp:GridView>
                        </td>
                        <td class="auto-style1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="ADD" />
                            <br />
                            <label>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;&gt;<br />
                            <br />
                            </label>
                            <br />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Button ID="btnRemove" runat="server" OnClick="btnRemove_Click"  Text="Remove" />
                            <br />
                            <label>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt;</label> </td>
                        <td valign="top">
                            <asp:GridView ID="gvTranferRows" runat="server" DataKeyNames="ModuleName" AutoGenerateColumns="false" CellPadding="5" EmptyDataText="No Records Found">
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkSelect2" runat="server" AutoPostBack="true" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                     <asp:BoundField DataField="id" HeaderText="id" />
                                    <asp:BoundField DataField="ModuleName" HeaderText="ModuleName" />
                                    <asp:BoundField DataField="Item" HeaderText="Item" />
                                    <asp:BoundField DataField="BatchNo" HeaderText="BatchNo" />
                                </Columns>
                                <HeaderStyle BackColor="#6699ff" Font-Bold="true" ForeColor="White" />
                            </asp:GridView>
                            <br />
    
                        </td>
                    </tr>
                </table>  
    

    .cs页面:

          protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        ddlbatch();
                        BindGridview();
                        BindSecondGrid();
                    }
                }
    public void ddlbatch()
            {
                SqlCommand cmd1 = new SqlCommand("Select BatchNo from Batches", con);
                cmd1.CommandType = CommandType.Text;
                cmd1.Connection = con;
                con.Open();
    
                ddlbatchno.DataSource = cmd1.ExecuteReader();
                ddlbatchno.DataTextField = "BatchNo";
                ddlbatchno.DataBind();
                con.Close();
                ddlbatchno.Items.Insert(0, new ListItem("--Select batche no--", "0"));
            }
    
            protected void BindGridview() //Binding gvDetaails gridview
            {
    
                SqlDataAdapter da = new SqlDataAdapter("select id,ModuleName,Item,BatchNo from ModuleItems", con);
                DataTable dt = new DataTable();
    
                da.Fill(dt);
                ViewState["dt"] = dt;
                ViewState["dt1"] = dt;
                gvDetails.DataSource = dt;
                gvDetails.DataBind();
            }
    
    
    
            protected void BindSecondGrid() // binding gvtransfer gridview
            {
                DataTable dt = (DataTable)ViewState["GetRecords"];
                gvTranferRows.DataSource = dt;
                gvTranferRows.DataBind();
    
            }
    
            string name; 
       protected void btnAdd_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in gvDetails.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox chkRow = (row.Cells[0].FindControl("chkSelect") as CheckBox);
                        if (chkRow.Checked)
                        {
    
                            int totalCount = gvDetails.Rows.Cast<GridViewRow>().Count(r => ((CheckBox)r.FindControl("chkSelect")).Checked);
    
                            name += row.Cells[1].Text + ",";
    
                        }
                    }
                }
                string[] name3 = name.Split(',');
    
                for (int l = 0; l < name3.Length; l++)
                {
    
                    string str = "UPDATE ModuleItems SET BatchNo = " + ddlbatchno.SelectedItem.ToString() + " WHERE id= '" + name3[l] + "'";
    
                    SqlCommand cmd = new SqlCommand(str, con);
                    con.Open();
    
                    int j = cmd.ExecuteNonQuery();
                    con.Close();
                }
                foreach (GridViewRow oItemLeft in gvDetails.Rows)
                {
                    if (((CheckBox)oItemLeft.FindControl("chkSelect")).Checked)
                    {
                        GetSelectedRows();
                        BindSecondGrid();
                        break;
                    }
    
                }
    
                DataTable dt = ViewState["dt"] as DataTable; //2nd time adding gvdetails to gvtransfer dt will become null
    
                if (dt != null)
                {
                    for (int i = dt.Rows.Count - 1; i >= 0; i--)
                    {
    
                        DataRow row = dt.Rows[i];
                        string dtname = row["id", DataRowVersion.Original].ToString();
                        string[] name1 = name.Split(',');
                        for (int l = 0; l < name1.Length; l++)
                        {
                            string name2 = name1[l].ToString();
                            if (dtname == name2)
                            {
                                row.Delete();
    
                            }
                        }
    
                    }
                }
    
    
                ViewState["dt"] = dt;
    
                gvDetails.DataSource = dt;
                gvDetails.DataBind();
            }   
            private void GetSelectedRows()
            {
                DataTable dt;
                if (ViewState["GetRecords"] != null)
                    dt = (DataTable)ViewState["GetRecords"];
                else
                    dt = CreateTable();
                for (int i = 0; i < gvDetails.Rows.Count; i++)
                {
                    CheckBox chk = (CheckBox)gvDetails.Rows[i].Cells[0].FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        dt = AddGridRow(gvDetails.Rows[i], dt);
                    }
    
                }
                ViewState["GetRecords"] = dt;
            }
            private DataTable CreateTable()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("ModuleName");
                dt.Columns.Add("Item");
                dt.Columns.Add("BatchNo");
    
                dt.AcceptChanges();
                return dt;
            }
    
            private DataTable AddGridRow(GridViewRow gvRow, DataTable dt)
            {
                DataRow[] dr = dt.Select("id = '" + gvRow.Cells[1].Text + "'");
                if (dr.Length <= 0)
                {
                    dt.Rows.Add();
                    int rowscount = dt.Rows.Count - 1;
                    dt.Rows[rowscount]["id"] = gvRow.Cells[1].Text;
                    dt.Rows[rowscount]["ModuleName"] = gvRow.Cells[2].Text;
                    dt.Rows[rowscount]["Item"] = gvRow.Cells[3].Text;
                    dt.Rows[rowscount]["BatchNo"] = ddlbatchno.SelectedItem.Text;
                    dt.AcceptChanges();
    
    
                }
                return dt;
            }
    
            protected void btnRemove_Click(object sender, EventArgs e)
            {
    
                if (gvTranferRows.Rows.Count > 0)
                {
                    foreach (GridViewRow row in gvTranferRows.Rows)
                    {
                        if (row.RowType == DataControlRowType.DataRow)
                        {
                            CheckBox chkRow1 = (row.Cells[0].FindControl("chkSelect2") as CheckBox);
                            if (chkRow1.Checked)
                            {
    
                                int totalCount = gvTranferRows.Rows.Cast<GridViewRow>().Count(r => ((CheckBox)r.FindControl("chkSelect2")).Checked);
    
                                name += row.Cells[1].Text + ",";
                            }
                        }
                    }
                    string[] name3 = name.Split(',');
                    foreach (GridViewRow oItemLeft in gvTranferRows.Rows)
                    {
                        if (((CheckBox)oItemLeft.FindControl("chkSelect2")).Checked)
                        {
                            GetRemoveRows();
    
                            BindGridview();
                            //break;
                        }
    
                    }
                    DataTable dt = ViewState["dt"] as DataTable;
                    for (int i = dt.Rows.Count - 1; i >= 0; i--)
                    {
    
                        DataRow row = dt.Rows[i];
                        string dtname = row["id", DataRowVersion.Original].ToString();
                        string[] name1 = name.Split(',');
                        for (int l = 0; l < name1.Length; l++)
                        {
                            string name2 = name1[l].ToString();
                            if (dtname != name2)
                            {
                                row.Delete();
    
                            }
                        }
    
                    }
    
    
                    ViewState["dt"] = dt;
    
                    gvTranferRows.DataSource = dt;
                    gvTranferRows.DataBind();
    
                }
            }
    
    
            DataTable dt,dt1;
    
            private void GetRemoveRows()
            {
    
    
                if (ViewState["GetRecords"] != null)
                    dt = (DataTable)ViewState["GetRecords"];
    
                   ViewState["GetRecords"] = dt;
                    ViewState["GetRecords2"] = dt;
                    gvDetails.DataSource = dt;
                    gvDetails.DataBind();
                this. ViewState.Remove("GetRecords2"); //for  viewstate control
    
                ViewState["GetRecords2"] = null;//for variables 
    
    
            }
    

    我正在附加我的结果图像结果将如何处理。我发布了完整的代码,任何人都可以帮助我。

    img

2 个答案:

答案 0 :(得分:0)

使用临时DataTable维护列表中选定的行或记录,然后使用DataTable绑定辅助GridView。

你会从这里得到更多的想法 Transfer Selected Rows from one GridView to Another in Asp.net

Another way to transfer data

答案 1 :(得分:0)

请尝试使用DataTable.ImportRow方法:

DataTable.ImportRow() on MSDN

我在许多WebForms和MVC页面上使用此方法,效果很好。