使用复选框C#将值从一个gridview添加到另一个gridview

时间:2014-01-30 07:01:04

标签: c# asp.net gridview checkbox row

我有两个GridView。 Gridview1有DataSource。我想在选中CheckBox时将它们的值传递给GridView2,但DataTable返回null。

这是我的代码:

public void getSelectRows()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Fabric"), new DataColumn("Roll"), new DataColumn("Batch"), new DataColumn("Width"), new DataColumn("Weight") });
    foreach (GridViewRow row in GridView1.Rows)
    {
       if (row.RowType == DataControlRowType.DataRow)
       {
          CheckBox chkRow = (row.Cells[5].FindControl("chkAdd") as CheckBox);
          if (chkRow.Checked)
          {
             string fab = row.Cells[0].Text;
             string roll = row.Cells[1].Text;
             string batch = row.Cells[2].Text;
             string fabwidth = row.Cells[3].Text;
             string weight = row.Cells[4].Text;
             dt.Rows.Add(fab, roll, batch, fabwidth, weight);
          }
       }
    }
    GridView2.DataSource = dt;
    GridView2.DataBind();
}

1 个答案:

答案 0 :(得分:0)

检查以下代码......

首先是aspx ......

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true" />
                    <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                    <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <asp:GridView ID="GridView2" runat="server">
            </asp:GridView>
        </div>
    </div>
    </form>
</body>
</html>

和.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("EmployeeID"), new DataColumn("FirstName"), new DataColumn("LastName") });
            dt.Rows.Add("1", "James", "Elgar");
            dt.Rows.Add("2", "Natasha", "Oliver");
            dt.Rows.Add("3", "Stein", "Sektnan");
            dt.Rows.Add("4", "Chris", "Massen");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        getSelectRows();
    }
    public void getSelectRows()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("EmployeeID"), new DataColumn("FirstName"), new DataColumn("LastName") });
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[3].FindControl("CheckBox1") as CheckBox);
                if (chkRow.Checked)
                {
                    string EmployeeID = row.Cells[0].Text;
                    string FirstName = row.Cells[1].Text;
                    string LastName = row.Cells[2].Text;
                    dt.Rows.Add(EmployeeID, FirstName, LastName);
                }
            }
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
}

希望它能解决你的问题...