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

本文关键字:网格 填充 一个 另一个 | 更新日期: 2023-09-27 17:50:41

我有一个grid with复选框,现在我想将Grid1选择的值存储到另一个网格grid2中。我该怎么做呢?My 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项

将此写入源文件

 <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。净

在Asp.net中将选定的行从一个GridView传输到另一个GridView