zoukankan      html  css  js  c++  java
  • 点击 Button触发事件将GridView1 CheckBox勾选的行添加到GridView2中

    有时候想实现一个CheckBox选取功能,但是很多细节不是很清楚

    相信大家都有遇到类似的情况,直接看代码,如下:

    前端代码GridView1,CheckBox控件设置

    <asp:GridView ID="GridView1" runat="server" Height="2px" Width="720px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="1" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="False" PageSize="8" >
    <RowStyle ForeColor="#000066" />
    <FooterStyle BackColor="White" ForeColor="#000066" />

    <Columns>

    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" Checked="false" />
    <%--<asp:Label ID="lbformid" runat="server" Visible="false" Text='<%# Eval("formid") %>'></asp:Label>--%>
    </ItemTemplate>
    <HeaderTemplate>
    <input type="checkbox" id="ChkHead" onclick="CheckAll3(this)" title="選擇全部" /><%--加上checked可自動勾選--%>
    </HeaderTemplate>
    <HeaderStyle Width="20px" />
    <ItemStyle Width="20px" />

    </asp:TemplateField>
                                <asp:BoundField DataField="data" HeaderText="日期(data)" ReadOnly="True" />
                                <asp:BoundField DataField="users" HeaderText="姓名(users)" SortExpression="姓名" />
                                <asp:BoundField DataField="user_name" HeaderText="賬號(user_name)"  />
                                <asp:BoundField DataField="user_email" HeaderText="郵箱(user_email)"  />
                                <asp:BoundField DataField="mony" HeaderText="金額(mony)" />

                 </Columns>
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="Freezing" />

     后台代码

    protected void Button3_Click(object sender, EventArgs e)
    {
    GridView2.DataSource = null; //數據定義成空值
    GridView2.DataBind();

    if (GridView1.Rows.Count < 1) //GridView1控件數據小於1行,執行該語句
    {
    MessageBox.Text = "請先查詢資料再匯入";
    MessageBox.ForeColor = System.Drawing.Color.Red; //獲取背景顏色
    return;
    }
    DataTable dte = new DataTable();
    dte.Columns.Add("data", typeof(string)); //獲取屬於該表列的集合
    dte.Columns.Add("users", typeof(string));
    dte.Columns.Add("user_name", typeof(string));
    dte.Columns.Add("user_email", typeof(string));
    dte.Columns.Add("mony", typeof(string));
    DataRow dr;

    ////建立相同架構的新數據
    //dr["data"] = "data";
    //dr["users"] = "";
    //dr["user_name"] = "";
    //dr["user_email"] = "";
    //dr["mony"] = "";
    int j=0;
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
    if (((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked) //默認是true(勾起)
    {
    dr = dte.NewRow();//創建具有相同架構表的新數據
    dr["data"] = GridView1.Rows[i].Cells[1].Text.ToString(); //獲取GridView1中列的位置,將數據綁定到建立的架構表相同的列
    dr["users"] = GridView1.Rows[i].Cells[2].Text.ToString();
    dr["user_name"] = GridView1.Rows[i].Cells[3].Text.ToString();
    dr["user_email"] = GridView1.Rows[i].Cells[4].Text.ToString();
    dr["mony"] = GridView1.Rows[i].Cells[5].Text.ToString();
    dte.Rows.Add(dr);
    j++;//增益性
    }

    }
    if (j<=0) //當excel中的MHour(月加班上限)小於或等於GridView1控件中第5行時,將判斷“時數必須大於已用時數!!”
    {
    MessageBox.Text = dte + "請選擇一項";
    MessageBox.ForeColor = System.Drawing.Color.Red;
    return;
    }

    GridView2.DataSource = dte;
    GridView2.DataBind();

    }

  • 相关阅读:
    Python+Selenium三种等待方法
    Jmeter结果分析_聚合报告
    Linux安装Python3
    翻译Go Blog: 常量
    Go: 复合数据类型slice
    Python创建二维列表的正确姿势
    了解Flask
    urllib3中学到的LRU算法
    了解Prometheus
    《redis 5设计与源码分析》:第二章 简单动态字符串
  • 原文地址:https://www.cnblogs.com/popo1/p/10012484.html
Copyright © 2011-2022 走看看