【问题标题】:Adding values from one gridview to another using checkbox C#使用复选框 C# 将值从一个网格视图添加到另一个网格视图
【发布时间】:2014-01-30 07:01:04
【问题描述】:

我有两个 GridView。 Gridview1 有数据源。当 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();
}

【问题讨论】:

  • datatable is returning null 你的意思是里面没有行?你能告诉我们你的Page_Load事件代码吗?
  • 没有page_load事件代码,但下面的答案是正确的。 :)

标签: c# asp.net gridview checkbox row


【解决方案1】:

检查下面的代码...

首先是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();
    }
}

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

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 2012-04-18
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多