如何将选定的值从另一个网格填充到网格中?

时间:2015-05-25 05:52:50

标签: c# gridview

我有一个带有复选框的Grid1,现在我想将Grid1选择的值存储到另一个网格grid2中。我怎样才能做到这一点? 我的Grid1是

<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center" DataKeyNames="ShiftID" 
Width="177px" onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChbGrid" runat="server"                                                        oncheckedchanged="ChbGrid_CheckedChanged" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="ChbGridHead" runat="server" AutoPostBack="True" 
Font-Bold="True" oncheckedchanged="ChbGridHead_CheckedChanged" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Gridview2是

 <asp:GridView ID="GridView2" runat="server" BackColor="#DEBA84" 
 BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
 CellSpacing="2">                                
 </asp:GridView>

我有一些功能

 protected void ChbGrid_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkstatus = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkstatus.NamingContainer;
    }
    protected void ChbGridHead_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkheader = (CheckBox)GridView1.HeaderRow.FindControl("ChbGridHead");
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkrow = (CheckBox)row.FindControl("ChbGrid");
            if (chkheader.Checked == true)
            {
                chkrow.Checked = true;
                {

                }
            }
            else
            {
                chkrow.Checked = false;
            }
        }

    }

   protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[1].Visible = false;
    }

我应该做些什么来改变我的预期输出。 My GridView1 conatins ShiftID,ShiftName,ShiftTime and Date。如何在Griview2

中为显示选中的Gridview1项生成查询

1 个答案:

答案 0 :(得分:2)

将此内容写入源文件

 <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />

单击Button时,将执行以下事件处理程序。在GridView数据行上执行循环,并引用CheckBox。

protected void GetSelectedRecords(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
            if (chkRow.Checked)
            {
                string name = row.Cells[1].Text;
                string country = (row.Cells[2].FindControl("lblCountry") as Label).Text;
                dt.Rows.Add(name, country);
            }
        }
    }
    gvSelected.DataSource = dt;
    gvSelected.DataBind();
}

有关详情,请点击此链接

GridView with CheckBox: Get Selected Rows in ASP.Net

Transfer Selected Rows from one GridView to Another in Asp.net