点击 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();
}